groupby implement in java

groupby implement in java

**import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Vector;
public class FileReading {
    /**
     * @param args
     */
    ///public  Hashtable<String, Object> isExistRecord(String oldCurrentLine,String unique,Vector<Integer> vec){
        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;
        String path = "D:\\groupBy.txt"; 
        int countR=0;
        try {
             String writepath = "D:\\groupBy.txt";     
             File file = new File(writepath);
             FileWriter fileWriter = new FileWriter(file,true); 
             String sCurrentLine;
             bufferFileWriter = new BufferedWriter(fileWriter);
             br = new BufferedReader(new FileReader(path));
             if(unique !=null && unique.equals("first")){
                ////System.out.println("Enter First Record --->"+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)) {
                ////System.out.println("sCurrentLine ----->"+sCurrentLine);


                    String arry[] = sCurrentLine.split("\\"+delimeter);
                    String oldarry[]  =oldCurrentLine.split("\\"+delimeter);
                    boolean groupBycolumnsCheckFlag = checkGroupByColumns(vec,arry,oldarry);
                    ////System.out.println("groupBycolumnsCheckFlag --->"+groupBycolumnsCheckFlag);
                    if(groupBycolumnsCheckFlag){
                        ////System.out.println("sCurrentLine record  -->"+sCurrentLine);
                        String cnt= arry[(arry.length-1)];
                        countR =Integer.parseInt(cnt);
                        countR = countR+1;
                        arry[(arry.length-1)] =countR+"";
                        ////System.out.println(oldCurrentLine+"-->old is  -->"+ arry[(arry.length-1)]);
                        //String finalStr = oldarry[0]+","+oldarry[1]+","+oldarry[2]+","+oldarry[3]+","+oldarry[4]+","+arry[(arry.length-1)];
                        String finalStr = UpdateSameRowWithCount(vec,oldarry,arry,delimeter);
                        //System.out.println("finalString record  -->"+finalStr);
                        bufferFileWriter.write(finalStr);
                        bufferFileWriter.newLine();
                        bufferFileWriter.flush();
                        isexist = true;
                        resultHash.put("isexist",true);
                        resultHash.put("currentRow",sCurrentLine);
                        closeConnections(br,bufferFileWriter);
                        /*br = null;
                        bufferFileWriter =null; */
                        temFileCreate(new BufferedReader(new FileReader("D:\\groupBy.txt")),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);
                }
            }
        }
        bufferFileWriter.write(oldCurrentLine);
        bufferFileWriter.newLine();
        bufferFileWriter.flush();
        resultHash.put("isexist",true);
        resultHash.put("currentRow",oldCurrentLine);
        return resultHash;
        } catch (IOException e) {
            e.printStackTrace();
            return resultHash;
        } finally {
            closeConnections(br,bufferFileWriter);
        }
    }
    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;
            }
        }
        return checkFlag;

    }
    public String UpdateSameRowWithCount(Vector<Integer> vec,String[] oldarry,String[] arry,String delimeter ){
        String updatedString = "";
        /*for(int index = 0;index<vec.size();index++ ){
            if(index ==0 || index== (vec.size()-1))
                updatedString += oldarry[index];
            else
                updatedString += oldarry[vec.get(index)]+",";

        }*/
        Vector<String> veci =new Vector<String>(Arrays.asList(oldarry)); 
        //System.out.println("Vector ------>"+veci);
        for(int index = 0;index<oldarry.length-1;index++ ){
            /*if(index ==0 || index== (vec.size()-1))
                updatedString += oldarry[index];
            else*/
                updatedString += oldarry[index]+delimeter;

        }
        if(arry!=null && arry.length>0)
            updatedString += arry[(arry.length-1)];
    //  //System.out.println("Prepare updatedString Count String --->"+updatedString);
        return updatedString;
    }
    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)
        {
            //System.out.println("While loop start  --->"+currenline1);
            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])))
            {
                //System.out.println("duplicate replace --->"+currenline1);
                bufferFileWritertemp1.write(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("Unique --->"+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 -->");
        }
        else{
            ////System.out.println("failure -->");
        }
    }
/*  public  Hashtable<String,Object >  groupByDataWriteToFile(String sCurrentLine,String unique,Vector<Integer> vec) throws IOException{
        Hashtable<String,Object > resultHash =isExistRecord(sCurrentLine,unique,vec);
        return resultHash;
    }
*/  
    public void prepareGroupBy(Vector<Integer> vec,String delimeter) {
        BufferedReader br = null;
        BufferedWriter bufferFileWriter  = null;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("D:\\completeprimarydups.txt"));
            int count =0;
            while ((sCurrentLine = br.readLine()) != null) {
                ////System.out.println("Enter into first phase");
                if(count ==0){
                    groupByDataWriteToFile(sCurrentLine+delimeter+"1","first",vec ,delimeter);
                }
                else {
                    //System.out.println("exixsting  Record--->"+sCurrentLine);
                    Hashtable<String,Object> resultHash = null;
                    if(sCurrentLine !=null&&sCurrentLine.toString().trim().length()>0)
                    {
                        resultHash =    groupByDataWriteToFile(sCurrentLine+delimeter+"1","same",vec,delimeter);
                    }
                //  //System.out.println("resultHash ===>"+resultHash);
             if(resultHash!=null && resultHash.size()>0) {
                 System.out.println("resultHash"+resultHash);
                 Boolean status = (Boolean) resultHash.get("isexist");
                 if(!status) {
                     if(sCurrentLine !=null&&sCurrentLine.toString().trim().length()>0)
                            groupByDataWriteToFile(sCurrentLine+delimeter+"1","unique",vec,delimeter );
                 }
             }
            }
                count++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                closeConnections(br,bufferFileWriter);
        }   
    }
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 static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
        Vector<Integer> vec = new Vector<Integer>();
        vec.add(0);
        vec.add(1);
        vec.add(2);
        vec.add(3);
        vec.add(4);
        FileReading fileReaderObj = new FileReading();
        String delimeter=",";
        fileReaderObj.prepareGroupBy(vec,delimeter);
        System.out.println("---Suceessfull Completed----");
    }
}**
View Answers

March 23, 2013 at 9:00 AM

**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:\\completeprimarydups.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);
            ex.printStackTrace();
        }
        finally {
            closeConnections(readoldfile,bufferFileWriter);              
        }
}
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");
    File file = new File("D:\\groupBy.txt");
    int countR=0;
    try {
          String sCurrentLine;
         //String writepath = "D:\\groupBy.txt";     
         //File file = new File("D:\\groupBy.txt"); 

              bufferFileWriter = new BufferedWriter(new FileWriter(file,true),8*1024);

        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 (Exception 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);
        //File file2 = new File("D:\\groupBy.txt"); 
        //bufferFileWriter = new BufferedWriter(new FileWriter(file2,true));
    }  
}
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);
    }
}**

March 23, 2013 at 9:00 AM

**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:\\completeprimarydups.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);
            ex.printStackTrace();
        }
        finally {
            closeConnections(readoldfile,bufferFileWriter);              
        }
}
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");
    File file = new File("D:\\groupBy.txt");
    int countR=0;
    try {
          String sCurrentLine;
         //String writepath = "D:\\groupBy.txt";     
         //File file = new File("D:\\groupBy.txt"); 

              bufferFileWriter = new BufferedWriter(new FileWriter(file,true),8*1024);

        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 (Exception 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);
        //File file2 = new File("D:\\groupBy.txt"); 
        //bufferFileWriter = new BufferedWriter(new FileWriter(file2,true));
    }  
}
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);
    }
}**









Related Tutorials/Questions & Answers:
groupby implement in java
groupby implement in java  **import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays
groupby implement in java
groupby implement in java  **import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays
Advertisements
groupby functionality in java
groupby functionality in java  import java.io.BufferedReader; import... java.util.ArrayList; class Groupby{ BufferedReader readexistingfile = null...){} } public static void main(String arg[]){ Groupby group = new Groupby
java: implement a class queue
java: implement a class queue  Implement a class Queue which supports the following operations: boolean isEmpty() Returns true if the queue is empty, and false otherwise. int peak() Returns the value at the front of a non
Java code to implement MULTIPATH INHERITANCE
Java code to implement MULTIPATH INHERITANCE  HI, I am new to java programming. I want to implement multipath inheritance in one java program... Please write code for above example to implement both
Java code to implement MULTIPATH INHERITANCE
Java code to implement MULTIPATH INHERITANCE  Hi, I am new to java .so please write code for this inheritance example... Student | ----------- | | Test Sports
How to implement FTP using java
How to implement FTP using java  Hi, I am a B.tech student and I want to implement FTP using Java to transfer files and exchange files between FTP client and FTP server. Could anyone help me for How to implement FTP using java
What is an efficient way to implement a singleton pattern in Java?
What is an efficient way to implement a singleton pattern in Java?  What is an efficient way to implement a singleton pattern in Java
How to implement openId java web based application?
How to implement openId java web based application?  In my web application i want to implement the OpenId just like stackoverflow.com have to login.... Please tell me how to implement it in java web application in details
Java Programming Implement a virtual print queue
Java Programming Implement a virtual print queue  Implement... records: q,3,10 is a q type record which indicates that a print job, # 3, shall... implement the queue insert and remove methods as described in the text. You must also
Java question to implement polymorphism - Java Beginners
Java question to implement polymorphism  I have a question that i need to answer:- Using the principle of polymorphism provide implementation for the calculateFees() method for each Online Fax Subscription type
How to implement a superclass Person? - Java Beginners
How to implement a superclass Person?  How to implement a superclass Person. Make two classes, Student and Lecturer, that inherit from Person... to implement the class..and how to write the toString method? please help me...  
How to implement this superclass Person - Java Beginners
How to implement this superclass Person  how to Implement a superclass Person which it need to Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major
Implement the Queue in Java
Implement the Queue in Java       In this section, you will learn how to implement the queue. A queue holds a collection of data or elements and follows the FIFO ( First In First
The implement keyword
The implement keyword       In java programming language, the keyword implement specifies... because java does not allow multiple inheritance, by it may implement more
Implement Java clients calling Web Services
Implement Java clients calling Web... the enterprise components Next    Implement Java clients calling Web Services Generating a Java client proxy and a sample
Implement transaction serializability
Implement transaction serializability  I need to implement transaction serializability in Java using semaphores/monnitors. What is the code to do
Implement push
Implement push  Hi.. How do you implement push on a flex applications? give me answer with example so i clearly understand Thanks  Ans: push implement on a flex applications using BlazeDS Server
Implement push
Implement push  hi....... just tell me about How do you implement push with flex data services? give me answer with example Thanks  Ans: Using BlazeDS Server and Live Cycle Data Services
implement microsoft office
implement microsoft office   how implement microsoft office in my java swing project ,or when microsoft office is not install then file open in defualt text editar.please send me source code
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.7.0 is released. Learn to use sermant-agentcore-implement version 0.7.0 in Maven based Java projects
-implement version 0.7.0 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.6.4 is released. Learn to use sermant-agentcore-implement version 0.6.4 in Maven based Java projects
-implement version 0.6.4 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.6.1 is released. Learn to use sermant-agentcore-implement version 0.6.1 in Maven based Java projects
-implement version 0.6.1 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 1.0.0 is released. Learn to use sermant-agentcore-implement version 1.0.0 in Maven based Java projects
-implement version 1.0.0 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.8.0 is released. Learn to use sermant-agentcore-implement version 0.8.0 in Maven based Java projects
-implement version 0.8.0 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 1.0.3 is released. Learn to use sermant-agentcore-implement version 1.0.3 in Maven based Java projects
; com.huaweicloud.sermant - sermant-agentcore-implement version 1.0.3 java library in your project... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.6.3 is released. Learn to use sermant-agentcore-implement version 0.6.3 in Maven based Java projects
-implement version 0.6.3 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.6.2 is released. Learn to use sermant-agentcore-implement version 0.6.2 in Maven based Java projects
-implement version 0.6.2 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
Maven dependency for com.huaweicloud.sermant - sermant-agentcore-implement version 0.9.0 is released. Learn to use sermant-agentcore-implement version 0.9.0 in Maven based Java projects
-implement version 0.9.0 java library in your project. ADS_TO_REPLACE_2 Now you can... of sermant-agentcore-implement released The developers of   com.huaweicloud.sermant - sermant-agentcore-implement project have released the latest
implement sale purchaes
implement sale purchaes  i want to implement a code in advance java with connectivity .. i have a table in database in which i have some stack of sms if customer puchases and enter value in text box the amount will be deduct
Implement the Serializable Interface
Implement the Serializable Interface  hii How many methods do u implement if implement the Serializable Interface?   hiii,ADS_TO_REPLACE_1... of its own to implement
Implement an interface in a JSP
Implement an interface in a JSP  Can we implement an interface in a JSP?   
Where to implement spring - Spring
Where to implement spring   Hi, Where we implement spring framework in j2ee appplication. I mean which layer .Thanks
Implement Drag and drop
Implement Drag and drop  Hi...... Please give the answer with example How do you implement drag and drop on components that do not support ondrag and ondrop? Thanks in advance   Ans: The example
implement core data iphone sdk
implement core data iphone sdk  How to implement core data in iPhone SDK? Please Help
Can an Interface implement another Interface?
Can an Interface implement another Interface?   Hi, Can an Interface implement another Interface? Thanks
Program to implement array of Objects.
Program to implement array of Objects.  Create a class it has following data members:- -name -account id -balance initialize the data members through constructors & define a print function & also define a function
Implement array of objects
Implement array of objects  import java.io.*; import java.util.*; public class dataa { DataInputStream in=new DataInputStream(System.in); int i; int a[i]=int acid[5]; String name1[i]=new String[5
program to implement array of objects
program to implement array of objects  import java.io.*; import java.util.*; public class dataa { DataInputStream in=new DataInputStream(System.in); int i; int a[i]=int acid[5]; String name1[i]=new String[5
Implement This Skeleton Code
Implement This Skeleton Code  package jp.co.worksap.global; import java.util.NoSuchElementException; /** * The Queue class represents an immutable first-in-first-out (FIFO) queue of objects. * @param <E> */ public class
Implement The Skeleton Code
Implement The Skeleton Code  package jp.co.worksap.global; import java.util.NoSuchElementException; /** The Queue class represents an immutable first-in-first-out (FIFO) queue of objects. @param <E> */ public class
Design and Implement GPA Calculator
Design and Implement GPA Calculator  Once the user clicks on the calculate button the program should display on a dialog box first the info about the student like his name. ID, session and student type. Second the program
how to implement spring security - Spring
how to implement spring security  Hai, iam working on small login application using hibernate on Spring. I wanted to implement spring security for my login application. Can any one tell me what is SpringSecurity, and how
what is a thread initial state? how to implement it?
what is a thread initial state? how to implement it?  what is a thread initial state? how to implement
Implement Date Class
Implement Date Class The class Date represents a specific instant in time. It allowed the interpretation of dates as year, month, day, hour, minute... are going to implement Date Class. Here is the code: public class Date
write a javascript progam to implement functions in html
write a javascript progam to implement functions in html  write a javascript progam to implement functions in html  Please visit the following link: http://www.roseindia.net/javascript/emailvalidation.shtml http
Implement Complex Number Class
Implement Complex Number Class You all are aware of Complex Numbers. These numbers consists of two parts,real and imaginary which can be written in the form... are going to implement Complex Number class and perform operations on it. Here
Implement method in Jsp - JSP-Interview Questions
Implement method in Jsp   Hi Friends, where should we implement methods in jsp(In webApplication.) in _jspService() method
implement internationlism in Flemish ans Spanish using Sturts1.1
implement internationlism in Flemish ans Spanish using Sturts1.1  HI - we are using struts1.1, we have a requirement to implement internationlism in Flemish ans Spanish. Will this be supported in Struts1.1? How to resolve
How to implement link in Web app lead to report with print and save features
How to implement link in Web app lead to report with print and save features   Hi friends, I developed a web application based on Java MVC architecture and Struts framework with Tomcat appserv, and I need to implement link lead

Ads