Saturday, 29 September 2018

Java program to WRITE A CSV FILE

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;


public class CSVwrite {
    public static void main(String[]args) throws FileNotFoundException{
        PrintWriter pw = new PrintWriter(new File("abc.csv"));
        StringBuilder sb = new StringBuilder();
        sb.append("id");
        sb.append(',');
        sb.append("Name");
        sb.append('\n');

        sb.append("1");
        sb.append(',');
        sb.append("sateesh bagadhi");
        sb.append('\n');

        pw.write(sb.toString());
        pw.close();
        System.out.println("done!");
    }
}


Java program to read CSV FILE

import java.io.*;

public class readfile {
 public static void main(String x[]) throws IOException{
   int i=0;
   BufferedReader b=new BufferedReader(new FileReader("cs.csv"));
   String[] user=new String[50000];
   String line="";
    while ((line = b.readLine()) != null)
    {
       user[i]=line;
       System.out.println(user[i]);   
       i++; 
    }
 }
}