编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔
package com.bwie.interview;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.StringTokenizer;
public class AnswerB01 {
public static void main(String[] args) throws IOException {
StringTokenizer tokenizer1 = getTokenzer("/a.txt");
StringTokenizer tokenizer2 = getTokenzer("/b.txt");
PrintStream out = new PrintStream("C:/c.txt");
while (tokenizer1.hasMoreTokens() && tokenizer2.hasMoreTokens()) {
out.println(tokenizer1.nextToken());
out.println(tokenizer2.nextToken());
}
out.close();
}
private static StringTokenizer getTokenzer(String fileName) throws IOException {
InputStreamReader reader = new InputStreamReader(AnswerB01.class.getResourceAsStream(fileName));
StringBuilder builder = new StringBuilder(1000);
int length = -1;
char[] cs = new char[1024];
while ((length = reader.read(cs)) != -1) {
builder.append(cs, 0, length);
}
reader.close();
return new StringTokenizer(builder.toString());