Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Development-process is there any possibelities fast read and write file large data file

Have Programming Question? Ask it here!
 
 
 


muralanagaraju
is there any possibelities fast read and write file large data file
0 Answer(s)      a year and 5 months ago
Posted in : Development process


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
public class Groupingclass {

public void prepareGroupBy(Vector<Integer> vec,String delimeter) {
    BufferedReader readoldfile = null;
    //BufferedWriter bufferFileWriter  = null;
        try{
            String sCurrentLine;
            // Groped file name (Suresh.txt)
            readoldfile = new BufferedReader(new FileReader("D:\\suresh.txt"));
            int count =0;
            while ((sCurrentLine = readoldfile.readLine()) != null) {
                if(count ==0){
                    groupByDataWriteToFile(sCurrentLine+delimeter+"1","first",vec ,delimeter);
                }
                else {                  
                    if(sCurrentLine !=null && sCurrentLine.toString().trim().length()>0)
                    {
                        /*Hashtable<String,Object> resultHash =*/ groupByDataWriteToFile(sCurrentLine+delimeter+"1","same",vec,delimeter);
                    /*
                    if(resultHash!=null && resultHash.size()>0) {
                         Boolean status = (Boolean) resultHash.get("isexist");
                         if(!status){
                             groupByDataWriteToFile(sCurrentLine+delimeter+"1","unique",vec,delimeter );
                         }
                     }*/
                    }
                } 
                count++;                
            }
        } catch(Exception ex){
            //File file = new File("D:\\groupby.txt");
            System.out.println("Exception>>>????????????????"+ex);
        }
        finally {
            try {
            readoldfile.close();
            //bufferFileWriter.close();
            }catch(IOException io){}
        }
}
public  Hashtable<String,Object >  groupByDataWriteToFile(String oldCurrentLine,String unique,Vector<Integer> vec,String delimeter) throws IOException
{
    Hashtable<String,Object> resultHash = new Hashtable<String,Object>();

    BufferedReader br = null;
    BufferedWriter bufferFileWriter  = null;
    boolean isexist = false;
    File path = new File("D:\\groupBy.txt");
    int countR=0;
    try {
          String sCurrentLine;
         String writepath = "D:\\groupBy.txt";     
         File file = new File(writepath);     
         bufferFileWriter = new BufferedWriter(new FileWriter(file,true));  

         if(path.exists()) {
                br = new BufferedReader(new FileReader(path));
         }
         else {
                    File gfile = new File("D:\\groupBy.txt");
                    br = new BufferedReader(new FileReader(gfile));
             }           
         if(unique !=null && unique.equals("first")) {
            // System.out.println("firs+++++tline :"+oldCurrentLine);
             bufferFileWriter.write(oldCurrentLine);
                bufferFileWriter.newLine();
                bufferFileWriter.flush();
                isexist = true;
                resultHash.put("isexist",true);
                //resultHash.put("currentRow",oldCurrentLine);
                return resultHash;
         }
         else {
             while ((sCurrentLine = br.readLine()) != null) {
                 //read from start original file
                 String arry[] = sCurrentLine.split(delimeter); 
                // it take row by row from original file
                 String oldarry[]  =oldCurrentLine.split(delimeter); 
                 boolean groupBycolumnsCheckFlag = checkGroupByColumns(vec,arry,oldarry);
                // if row is same true
                 if(groupBycolumnsCheckFlag) {
                        String cnt= arry[(arry.length-1)];
                        countR =Integer.parseInt(cnt);
                        countR = countR+1;
                        arry[(arry.length-1)] =countR+"";

                        String finalStr = UpdateSameRowWithCount(vec,oldarry,arry);
                        // System.out.println("finalStr :"+finalStr);
                        bufferFileWriter.write(finalStr);
                        bufferFileWriter.newLine();
                        bufferFileWriter.flush();
                        isexist = true;
                        resultHash.put("isexist",true);
                        resultHash.put("currentRow",sCurrentLine);
                        closeConnections(br,bufferFileWriter);
                        if(path.exists()) {
                        temFileCreate(new BufferedReader(new FileReader("D:\\groupBy.txt")),oldCurrentLine,countR,vec,delimeter);
                        }
                        else {
                            File gfile = new File("D:\\groupBy.txt");
                            temFileCreate(new BufferedReader(new FileReader(gfile)),oldCurrentLine,countR,vec,delimeter);
                        }
                        resultHash.put("isexist",true);
                        resultHash.put("currentRow",sCurrentLine);
                        return resultHash;
                 }
                 else if(!sCurrentLine.equals(oldCurrentLine)) {
                        resultHash.put("isexist",isexist);
                        resultHash.put("currentRow",sCurrentLine);
                    }
             }          
         }
    // System.out.println("not same :"+oldCurrentLine);
         if(file.exists()) {
            bufferFileWriter.write(oldCurrentLine);
            bufferFileWriter.newLine();
            bufferFileWriter.flush();
            resultHash.put("isexist",true);
            resultHash.put("currentRow",oldCurrentLine);
         }
         else{
             File file1 = new File("D:\\groupBy.txt"); 
             bufferFileWriter = new BufferedWriter(new FileWriter(file1,true));
             bufferFileWriter.write(oldCurrentLine);
                bufferFileWriter.newLine();
                bufferFileWriter.flush();
                resultHash.put("isexist",true);
                resultHash.put("currentRow",oldCurrentLine);
         }
            return resultHash;
   } catch (IOException e) {
        /*System.out.println("Exception-----------------"+e);
        e.printStackTrace();*/
    //   File file2 = new File("D:\\groupBy.txt"); 
  //     bufferFileWriter = new BufferedWriter(new FileWriter(file2,true));
        return resultHash;
    } finally {
        closeConnections(br,bufferFileWriter);
    }  
}
public  void temFileCreate(BufferedReader br,String currentLine,int count,Vector<Integer> vec,String delimeter)throws IOException
{
    BufferedWriter  bufferFileWritertemp1=null;
    bufferFileWritertemp1 = new BufferedWriter(new FileWriter("D:\\groupBytemp.txt"));
    String[] arry=currentLine.split(delimeter);
    String currenline1="";
    String[] oldarry=null;
    while((currenline1=br.readLine())!=null)
    {
        oldarry=currenline1.split(delimeter);
        boolean groupBycolumnsCheckFlag = checkGroupByColumns(vec,arry,oldarry);
        //if((arry[0].equals(oldarry[0]))&&(arry[1].equals(oldarry[1]))&&(arry[2].equals(oldarry[2]))&&(arry[3].equals(oldarry[3]))&&(arry[4].equals(oldarry[4]))&&(count==Integer.parseInt(oldarry[oldarry.length-1])))
        if(groupBycolumnsCheckFlag&&(count==Integer.parseInt(oldarry[oldarry.length-1])))
        {
            bufferFileWritertemp1.write(currenline1);
            //System.out.println("tempfile line :"+currenline1);
            bufferFileWritertemp1.write("\n");
        }
        //else if(!((arry[0].equals(oldarry[0]))&&(arry[1].equals(oldarry[1]))&&(arry[2].equals(oldarry[2]))&&(arry[3].equals(oldarry[3]))&&(arry[4].equals(oldarry[4]))))
        else if(!groupBycolumnsCheckFlag)
        {
            //System.out.println("temp file not equal :"+currenline1);
            bufferFileWritertemp1.write(currenline1);
            bufferFileWritertemp1.write("\n");
        }
    }   
    closeConnections(br,bufferFileWritertemp1);
    /*br = null;
    bufferFileWritertemp1 = null;*/
    File newfile1 =new File("D:\\groupBy.txt");
    if(newfile1.delete()) {
        //System.out.println(newfile1.getName() + " is deleted!");
    } else {
        //System.out.println(newfile1.getName() +"Delete operation is failed.");
    }
    File oldfile =new File("D:\\groupBytemp.txt");
    File newfile =new File("D:\\groupBy.txt");
    if(oldfile.renameTo(newfile)){
        System.out.println("success -->");
        //System.gc();
    }
    else { 
        File newfile2 =new File("D:\\groupBy.txt");
        if(oldfile.renameTo(newfile2)){
            System.out.println("???????????????success -->");
        }
    }
}
public void closeConnections(BufferedReader br,BufferedWriter bw)
{
    try {
        if(br!=null)
        {
            try {
            br.close();
            br = null;
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
        if(bw!=null)
        {
            bw.close();
            bw = null;
        }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
public String UpdateSameRowWithCount(Vector<Integer> vec,String[] oldarry,String[] arry) {
    String updatedString = "";
    for(int index = 0;index<vec.size();index++) {
        updatedString += oldarry[vec.get(index)]+",";       
    }
    if(arry!=null && arry.length>0)
        updatedString += arry[(arry.length-1)]; 
    return updatedString;
}
public boolean checkGroupByColumns(Vector<Integer> vec,String[] currentRowArray,String[] checkRowArray) {
    boolean checkFlag = true;
    for(int index = 0;index<vec.size();index++) {
        if(!currentRowArray[index].toString().trim().equals(checkRowArray[index].toString().trim())){
            checkFlag = false;
            return checkFlag; // when index is not matched
        }       
    }
    return checkFlag;   
}
public static void main(String[] args) {
        Groupingclass groupby = new Groupingclass();
        Vector<Integer> vec = new Vector<Integer>();
        vec.add(0);
        vec.add(1);
        vec.add(2);
        vec.add(3);
        vec.add(4);
        String delimeter=",";
        System.out.println("call prepareGroupBy method");
        groupby.prepareGroupBy(vec,delimeter);
    }
}

Advertisement
View Answers

Related Tutorials/Questions & Answers:
 

 

 

DMCA.com