编写一个程序,讲a.txt文件中的单词与b.txt文件中单词与b.txt文件中的单词交替合并到c.txt文件中, a.txt文件中的单词用回车符分割,b.txt文件中用回车或空格进行分隔
package praDemo;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
/**
* 编写一个程序,讲a.txt文件中的单词与b.txt文件中单词教体结合并到c.txt文件中,
* a.txt文件中的单词用回车符分割,b.txt文件中用回车或空格进行分隔
* @author Administrator
*
*/
public class AlgorithmDemo {
public static void main(String[] args) throws Exception {
FileManager a = new FileManager("F:\a.txt",new char[] {'
'}); //文件所在的路径
FileManager b = new FileManager("F:\b.txt",new char[] {'
'}); //文件所在的路径
FileWriter c = new FileWriter("F:\c.txt");
String aWord = null;
String bWord = null;
while((aWord = a.newxtWord())!=null){
c.write(aWord+"
");
bWord = b.newxtWord();
if(bWord != null) {
c.write(bWord+"
");
}
while((bWord = b.newxtWord())!=null) {
c.write(bWord+"
");
}
c.close();
}
}
}
class FileManager{
String[] words = null;
int pos = 0;
public FileManager(String filename,char[] sperators)throws Exception{
File f = new File(filename);
FileReader reader = new FileReader(f);
char[] buf = new char[(int)f.length()];
int len = reader.read(buf);
String results = new String(buf,0,len);
String regex = null;
if(sperators.length >1) {
regex = ""+sperators[0]+"|"+sperators[1];
}else {
regex = ""+sperators[0];
}
words = results.split(regex);
}
public String newxtWord() {
if(pos == words.length)
return null;
return words[pos++];
}
}