Saturday, January 22, 2011

Write a Program in java for selecting Special Columns in a file which separated by space



/* open a file which includes columns separeted by "  "  and copy all content in other file  which  fields  separated by "," */

import  java.io.*;
import  java.util.*;

class SeparateColumns {
      public static void main(String  args[]){
            File inFile= new File("G:/Sign-Code/cpi.txt");
            File outFile=new File("G:/Sign-Code/cccpi.txt");
            FileReader ins= null;
            FileWriter outs=null;
            BufferedReader br=null;
            BufferedWriter bw=null;
           
            try { ins=new  FileReader(inFile);
                  outs=new FileWriter(outFile);
                  br=new BufferedReader(ins);
                  bw=new BufferedWriter(outs);
                  String temp[]=new String[7];  // shows number of column=7
                  String str;
                  while((str=br.readLine())!=null){
                    temp= str.split("\t");
                    bw.write(temp[0]);
                    bw.append(",");
                    bw.write(temp[1]);
                    bw.append(",");
                    bw.write(temp[2]);
                    bw.append(",");
                    bw.write(temp[3]);
                    bw.append(",");
                    bw.write(temp[4]);
                    bw.append(",");
                    bw.write(temp[5]);
                    bw.append(",");
                    bw.append(temp[6]);
                    bw.newLine();}
                    }catch(IOException e){}
                    finally {
                          try {
                                br.close();
                                bw.close();} catch(IOException e){}
                                            
                                    
      }          
      }
      }

No comments:

Post a Comment