简体   繁体   English

使用Filewriter将新文件写入CSV文件

[英]writing new file using filewriter to csv file

I am working on a CSV file having 45312 instances(row) and 8 attributes. 我正在处理具有45312个instance(row)和8个属性的CSV文件。 I want to modify (using some mathematical operation) the input csv file and write it. 我想修改(使用一些数学运算)输入的csv文件并将其写入。 To modify the CSV I have extracted each column value (v="comma separated position") from every instance(row) and then try to modify it. 要修改CSV,我从每个实例(行)中提取了每个列值(v =“逗号分隔的位置”),然后尝试对其进行修改。 But I am facing the problem here that when the for loop moves to next iteration (like v=3 from v=2) then all modified instance values from the previous iteration (v=2) are reverted back to their original state. 但是我在这里面临的问题是,当for循环移至下一个迭代时(例如v = 2的v = 3),那么前一次迭代的所有修改后的实例值(v = 2)都将恢复为原始状态。 At the end I want the new CSV file with all modifications processed. 最后,我要处理所有修改的新CSV文件。

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.*; 
    public class Normalization {
     public static void main(String[] args)throws IOException {
            String filename = "abc.csv";
            File file = new File(filename);
            BufferedWriter writer = null;
    try{           
             writer = new BufferedWriter(new   FileWriter("lyupdated.csv"));      
        for (int v = 1; v < 8; v++)
        {
                Scanner inputStream = new Scanner(file);
          while (inputStream.hasNext())
          {
                String data = inputStream.next();         
                String[] values = data.split(",");
                double balance = Double.parseDouble(values[v]);
                balance=balance*10;//for mathoperation code simplification
                values[v] = String.valueOf(balance);      
                // iterate through the values and build a string out of them
                StringBuilder sb = new StringBuilder();
                //  String newData = sb.toString();
               for (int i = 0; i < values.length; i++) {
                        sb.append(values[i]);
                        if (i < values.length - 1) {
                            sb.append(",");
                        }
                    }
          System.out.println(sb.toString());
          writer.write(sb.toString()+"\n");
           }inputStream.close();           
        } writer.close();
     }
          catch (FileNotFoundException ex) {
                Logger.getLogger(Normalization.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    }

Your code doesn't make much sense. 您的代码没有多大意义。 You need to open the file, have a loop to read the file line by line, and split each line into 8 columns. 您需要打开文件,有一个循环来逐行读取文件,然后将每行分成8列。

But instead, look at what you have: 但是,请查看您拥有的内容:

for (int v = 1; v < 8; v++) {
    Scanner inputStream = new Scanner(file);

So, 所以,

  • you don't loop over all the lines of the file, but over 7 columns 您不会遍历文件的所有行,但会遍历7列
  • at each of the 7 iterations, you open a new Scanner over the file, thus always reading from the start of the file 在这7次迭代中的每一次迭代中,您都在文件上打开一个新的Scanner,因此始终从文件的开头进行读取

As Tom says in his comment, use a library dedicated to CSV file parsing and generation. 正如Tom在评论中所说,请使用专用于CSV文件解析和生成的库。 It will have the following advantages: 它具有以下优点:

  • being correct 是正确的
  • being tested by many developers, and optimized 经过许多开发人员的测试,并进行了优化
  • offering a higher-level API 提供更高级别的API
  • handling escaping of values correctly 正确处理值的转义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM