import java.io.*; class soln10mon { public static void main(String[] args) throws Exception { bubblesort b=new bubblesort(); b.sort(); } void sort() throws Exception { File f1=new File("FileA.TXT"); File f2=new File("FileB.TXT"); boolean change=true; int alternate=0; while(change) { alternate=(alternate+1)%2; change=false; // intialization of File io FileInputStream fis=new FileInputStream(f1); // result of previous iteration FileWriter fw=new FileWriter(f2);// result for next iteration StreamTokenizer st=new StreamTokenizer(fis); st.nextToken(); int current=(int)st.nval,next=0; while(st.nextToken()!=st.TT_EOF) { next=(int)st.nval; if(next there is next iteration fw.write(""+next+" "); System.out.println(" writing"); } else{// Change the current System.out.println(" writing"); fw.write(""+current+" "); current=next; } } fw.write(""+current+" "); // write last element fw.flush(); fw.close(); // change the File references for each alternation if(alternate==1) { f1=new File("FileB.TXT"); f2=new File("FileA.TXT"); } else { f1=new File("FileA.TXT"); f2=new File("FileB.TXT"); } }// outer while if(alternate==1) { f2.renameTo(f1);} // if result is in FileB then copy it back to FileA } };