**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----"); } }**
Ads