Thursday, August 18, 2016

csv file processing in java


 */
/**/package excel
import java.io.*;
import java.util.StringTokenizer;

public class Cl1 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

String name;
String roll;
int marks;

File f = new File("C:\\Users\\user\\Documents\\cvv2.csv"); // file path, please change as per your requirements
int ss = size(f);
System.out.println(ss);
String sen[][] = new String [ss][2];
//get the classes ready for file reading

FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new  InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

//-------------------------------------------

String line="";
int index=0;

while((line=br.readLine())!=null){ //read each line
StringTokenizer t = new StringTokenizer(line,","); // split the words
while(t.hasMoreTokens()){
sen[index][0] = t.nextToken();  // store first  word in the zeroth position of the matrix
sen[index++][1] = t.nextToken();// store second word in the first position of the matrix


}

}
//close all the necessary file objects.
fis.close();
   isr.close();
   br.close();
   //modify
   FileWriter fr = new FileWriter(f); // open file writer
   System.out.println("Increment roll numbers by  and store again: ");
   fr.write("Name,Roll\n"); // write the header
   int j=1;
   while(j<ss){
    //sen
    sen[j][1] = Integer.toString(Integer.parseInt(sen[j][1])+5);
    fr.write(sen[j][0]+","+sen[j++][1]+"\n");
   }
   fr.close();
   read(f);
   //modify
 
 
}
static void read(File f) throws IOException{ // function to display file content
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new  InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine())!=null){
System.out.println(line);
}
fis.close();
br.close();
isr.close();

}
static int size(File ff){// function to get the total lines in the file including the header
File f = ff;
int si = 0;
FileInputStream fis;
try {
System.out.println(f);
fis = new FileInputStream(f);
InputStreamReader isr = new  InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line="";

while((br.readLine())!=null){
si++;
}

fis.close();
   isr.close();
   br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("gggg");
e.printStackTrace();
}

return si;
}

}

arraylist with iterator in java


package p1;
import java.io.File;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sujith
 */
public class fileprgm {
 
    public static void main(String[] args) {
        File f1 = new File("e:\\data6.csv");
        try {
            BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(f1)));
            String line;
            List<String> ar = new ArrayList<String>();
            while((line=br.readLine())!=null){
                StringTokenizer st= new StringTokenizer(line,",");
                while(st.hasMoreTokens()){
             
                 
                    ar.add(st.nextToken());
                         
                }
             
            }
         

            Iterator<String> crunchifyIterator = ar.iterator();
while (crunchifyIterator.hasNext()) {

                        if(crunchifyIterator.hasNext()){
                           System.out.print(crunchifyIterator.next()+" ");
                        }
                        if(crunchifyIterator.hasNext()){
                           System.out.print(crunchifyIterator.next()+" ");
                        }
                        if(crunchifyIterator.hasNext()){
                           System.out.print(crunchifyIterator.next()+" ");
                        }

                        System.out.print("\n");
}
        } catch (Exception ex) {
            Logger.getLogger(msuppu.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
}

file prgm csv,txt handling

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class add_csv {
    public static void main(String[] args) {
        File f1= new File("E://data4.txt");
         BufferedReader br,br2;
         int size=0;
        try {
            br = new BufferedReader(new FileReader(f1));
              String line;
              int sum=0;
            while((line=br.readLine())!=null){
                StringTokenizer st= new StringTokenizer(line,",");
                while(st.hasMoreTokens()){
                    String p=st.nextToken();
                    //sum+=Integer.parseInt(p);
                    System.out.println(p);
                    size++;
                }
   
            }
            br.close();
            br2 = new BufferedReader(new FileReader("E://data4.txt"));
            FileOutputStream os= new FileOutputStream("E://copy4.txt");
            String[] myst= new String[size];
            int i=0;
            while((line=br2.readLine())!=null){
                 StringTokenizer st= new StringTokenizer(line,",");
                  while(st.hasMoreTokens()){
                    String p=st.nextToken();
                    myst[i++]=p;
                      //System.out.println(i);
                }
            }
            System.out.println(" ");
            for(int k=0;k<size;k++){
                for(int j=k+1;j<size;j++){
                    if(myst[k].compareTo(myst[j])>0){
                    String temp=myst[k];
                       // System.out.println("hi");
                    myst[k]=myst[j];
                    myst[j]=temp;
                    }
                }
            }
            for(String pintu:myst){
              System.out.println(pintu);  
              os.write(pintu.getBytes());
              os.write(" \n".getBytes());
            }
            System.out.println("The sum of integer is "+sum);  
        } catch (Exception ex) {
            Logger.getLogger(add_csv.class.getName()).log(Level.SEVERE, null, ex);
        }
         
    }
}

rough work 1 file read and write

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;

import java.io.BufferedReader;
import java.io.File;
import java.io.*;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 *
 * @author sujith
 */
public class str3 {
    public static void main(String[] args){
        File file= new File("C:\\data3.txt");
        File file2=new File("C:\\data3.txt");
       // String[] tokens = null;
        int k=0;
        try{
        //BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            // BufferedReader br= new BufferedReader(new FileReader(f));
            BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            FileInputStream in= new FileInputStream("C:\\data3.txt");
            FileOutputStream op= new FileOutputStream("E:\\data5.txt");
            //InputStreamReader ip=new InputStreamReader(in);          
            //OutputStreamWriter op= new OutputStreamWriter(new FileOutputStream("E:\\data4.txt"));
            //BufferedReader br= new BufferedReader(in);
           // System.out.println(ip);
            while((k=in.read())!=-1){
                System.out.println((char)k);
               // op.write(k);
               // op.append((char)k);
               // System.out.println((byte)k);
              
            }
        String line=null;
        int count=0;
        int i=0;
       
        while((line=br.readLine())!=null){
            //tokens=line.split("\\s+");
            StringTokenizer st= new StringTokenizer(line," ");
          
           while(st.hasMoreTokens()){
               String s=st.nextToken();
               char c;
               StringBuffer sb=new StringBuffer(s);
              for(int j=0;j<sb.length();j++){
                  for(int m=j+1;m<sb.length();m++){
                     // System.out.println(sb.ch);
                      //sb.setCharAt(i, ch);
                      if(sb.charAt(j)>sb.charAt(m)){
                          c=sb.charAt(j);
                          sb.setCharAt(j,sb.charAt(m));
                          sb.setCharAt(m, c);
                      }
                    
                  }
              }
              System.out.println(sb);
               //System.out.println(st.nextToken());             
               //StringBuffer sb=new StringBuffer(st.nextToken());
               // StringBuilder sb= new StringBuilder(st.nextToken());
               //String sb=st.nextToken();
              // String[] sb=new String[]{st.nextToken()};
               //System.out.println(sb.length);
              // System.out.println(st.nextToken());
              
              // op.write(st.nextToken().getBytes());
              // op.write(" ".getBytes());
              
               //System.out.println(st.nextToken().getClass().getName());
             
               op.write(sb.toString().getBytes());
               op.write(" ".getBytes());
           }
            op.write("\n".getBytes());
          
        }
       

        }catch(Exception e){
            System.out.println(e);
        }
    }
   
}