【问题标题】:I want to update CSV file with a different value for each line, code is returning the same value though我想用每行不同的值更新 CSV 文件,但代码返回相同的值
【发布时间】:2014-06-09 13:41:38
【问题描述】:

我的 file.csv 包含

**narendra.koribilli@uni.com**  
**stefan.hummel@uni.com**       
**ibrsduser**                      
**michel.csaba@uni.com**        
**wsdl**                           
**lalit.choudhary@uni.com**     
**Remedy Application Service**     
**vinay.k.kedlaya@uni.com** 

我想将这些内容写入另一个 .csv 文件,例如:

**narendra.koribilli@uni.com**,    **some random_value()**

**stefan.hummel@uni.com**, **some random_value()**      

**ibrsduser**,**some random_value()**                      

**michel.csaba@uni.com**,**some random_value()**        

**wsdl**,**some random_value()**                           

**lalit.choudhary@uni.com**,**some random_value()**    

**Remedy Application Service**,**some random_value()**    

**vinay.k.kedlaya@uni.com**,**some random_value()**

在上面几行中,一些 random_value() 是为第一个字符串生成随机字符串的方法(即 narendra.koribilli@uni.com)

这是我尝试过的,但它为所有不同的员工姓名打印相同的随机值

这是我迄今为止尝试过的

String KeyValue="";

private static void getString() 抛出 IOException

{

    PrintStream out = new PrintStream("D:\\fileUpdate.csv");

    BufferedReader br=new BufferedReader(new FileReader("D:\\file.csv"));

    String str=null;

    while((str=br.readLine())!=null)
    {
        String fetch=Generate_StringForUSer(str);
        out1.println(str+","+fetch);

    }           

}

private static String Generate_StringForUSer(String str) {

    int keylen=str.length();

    while (KeyValue.length () != keylen)
    {
        int rPick = r.nextInt(4);
        if (rPick == 0)
        {
            int spot = r.nextInt(25);
            KeyValue += dCase.charAt(spot);
        } 
        else if (rPick == 1) 
        {
            int spot = r.nextInt (25);
            KeyValue += uCase.charAt(spot);
        } 
        else if (rPick == 2) 
        {
            int spot = r.nextInt (7);
            KeyValue += sChar.charAt(spot);
        } 
        else if (rPick == 3)
        {
            int spot = r.nextInt (9);
            KeyValue += intChar.charAt (spot);
        }
    }
    System.out.println (KeyValue);
    return KeyValue;

}

输出是这样的......

6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m
6Tui3ClnNqk#$8gCEAHxA3Er0U!V$5m

为所有用户生成相同的随机字符串..不同名称应该不同...请告诉我哪里错了..谢谢大家

【问题讨论】:

    标签: java csv


    【解决方案1】:

    您在读取文件时使用了不同的缓冲阅读器。

    注意

    中的1
    while((str=br1.readLine())!=null)
    

    您在哪里声明 BufferedReader br

     BufferedReader br=new BufferedReader(new FileReader("D:\\file.csv"));
    

    把上面的while语句改成

    while((str=br.readLine())!=null)
    

    生成一个给定长度的字符串,包含随机字符,

    选项 1 使用 org.apache.commons.lang 's RandomStringUtils 类。

    RandomStringUtils.random(lengthOfRequiredStr)

    选项 2 如果您不想依赖公共库,请自己编写代码 - 请参阅此 link

    【讨论】:

    • @codeMan..是的,我在我的代码中提到过,在这里我错误地忘记了提到“br1”。没关系,但我没有得到不同名称的不同随机值..
    • @user3711467 将您在 while 循环中从 br 读取的每一行打印到控制台,并确保您正确获取文件的内容。
    • @user3711467 KeyValue 的类型是什么?
    • keyValue 类型为 String.. String KeyValue="";
    • 你生成随机数的逻辑是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多