write csv file line by line using string in java

vivek patel picture vivek patel · Feb 4, 2015 · Viewed 8.1k times · Source

I had a modify my original csv. now I want modified csv file whether it is generate new csv file or updated on it ...how could it possible? I had a extract a single column values of csv and incremented by 1. now I want csv file with modified data. how can I write file.

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);
        }        
             }

Answer

Naren picture Naren · Feb 4, 2015

This way you can write new updated csv file..

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);
        }


    }
}