使用java中的字符串逐行编写csv文件

时间:2015-02-04 05:02:09

标签: java csv filestream filewriter stringwriter

我修改了原来的csv。现在我想修改csv文件,无论是生成新的csv文件还是更新它...怎么可能? 我有一个提取单个列的值csv并递增1.现在我想要csv文件与修改过的数据。我怎么写文件。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.*;
   public class Main {

    public static void main(String[] args) throws IOException {

        String filename ="bank-full.csv";

        File file= new File(filename);        
        try {           
           Scanner inputStream = new Scanner(file);
           inputStream.next();           
             while(inputStream.hasNext())
            {

                String data= inputStream.next();                 
                String[] values = data.split(";");
                double balance= Double.parseDouble(values[11]);
                balance=balance+1;                       

                values[11] = String.valueOf(balance);

// iterate through the values and build a string out of them
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < values.length; i++) {
                    sb.append(values[i]);
                    if (i < values.length - 1) {
                        sb.append(";");
                    }
                }
                // get the new string
                String newData = sb.toString();

                  BufferedWriter writer = null;
try
{
    writer = new BufferedWriter( new FileWriter( "bank-full.csv"));
    writer.write( newData);

}
catch ( IOException e)
{
}
finally
{
    try
    {
        if ( writer != null)
        writer.close( );
    }
    catch ( IOException e)
    {
    }
}
        System.out.println(newData);
            }           
                inputStream.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }        
             }

1 个答案:

答案 0 :(得分:0)

这样您就可以编写新的更新的csv文件..

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {

        String filename = "src//bank-full.csv";

        File file = new File(filename);

        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter("bank-full_updated.csv"));


        } catch (IOException e) {
        } 
            try {
            Scanner inputStream = new Scanner(file);
        //  inputStream.next();
            while (inputStream.hasNext()) {

                String data = inputStream.next();
                String[] values = data.split(";");
                double balance = Double.parseDouble(values[2]);
                balance = balance + 1;



                values[2] = 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(";");


                    }

                }
                // get the new string

                System.out.println(sb.toString());
                writer.write(sb.toString()+"\n");
                }
            writer.close();
            inputStream.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}