1 package sundemo2; 2 3 import java.io.File; 4 import java.io.FileReader; 5 import java.io.FileWriter; 6 7 public class MainClass{ 8 public static void main(String[] args) throws Exception{ 9 FileManager a = new FileManager("E:\WorkSpace\SunDemo\data\demo\a.txt",new char[]{' '}); 10 FileManager b = new FileManager("E:\WorkSpace\SunDemo\data\demo\b.txt",new char[]{' ',' '}); 11 FileWriter c = new FileWriter("E:\WorkSpace\SunDemo\data\demo\c.txt"); 12 String aWord = null; 13 String bWord = null; 14 while((aWord = a.nextWord()) !=null ){ 15 c.write(aWord + " "); 16 bWord = b.nextWord(); 17 if(bWord != null) 18 c.write(bWord + " "); 19 } 20 21 while((bWord = b.nextWord()) != null){ 22 c.write(bWord + " "); 23 } 24 c.close(); 25 } 26 } 27 28 class FileManager{ 29 String[] words = null; 30 int pos = 0; 31 public FileManager(String filename,char[] seperators) throws Exception{ 32 File f = new File(filename); 33 FileReader reader = new FileReader(f); 34 char[] buf = new char[(int)f.length()]; 35 int len = reader.read(buf); 36 String results = new String(buf,0,len); 37 String regex = null; 38 if(seperators.length > 1 ){ 39 regex = "" + seperators[0] + "|" + seperators[1]; 40 }else{ 41 regex = "" + seperators[0]; 42 } 43 words = results.split(regex); 44 } 45 46 public String nextWord(){ 47 if(pos == words.length) 48 return null; 49 return words[pos++]; 50 } 51 }