package cn1;
import java.io.FileWriter;
/** 金航数码科技有限责任公司 </p>
* 作者: Administrator </p>
* 邮箱: xxx@avicit.com </p>
* 创建时间: 2013-4-16 上午09:48:32 </p>
* 类说明: </p>
* 修改记录: </p>
*/
public class itcast {
/**请添加说明文字
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
FileManager a = new FileManager("d:/za.txt", new char[]{'\n', ' '});
FileManager b = new FileManager("d:/zb.txt", new char[]{'\n', ' '});
FileWriter c = new FileWriter("d:/zc.txt");
String aWord = null;
String bWord = null;
while ((aWord = a.nextWord()) != null) {
c.write(aWord + "\n");
bWord = b.nextWord();
if (bWord != null)
c.write(bWord + "\n");
}
while ((bWord = b.nextWord()) != null) {
c.write(bWord + "\n");
}
c.close();
}
}
package cn1;
import java.io.File;
import java.io.FileReader;
/** 金航数码科技有限责任公司 </p>
* 作者: Administrator </p>
* 邮箱: xxx@avicit.com </p>
* 创建时间: 2013-4-16 上午10:01:47 </p>
* 类说明: </p>
* 修改记录: </p>
*/
public class FileManager {
String[] words = null;
int pos = 0;
public FileManager(String filename, char[] seperators) throws Exception {
File f = new File(filename);
FileReader reader = new FileReader(f);// 将字符读入数组。在某个输入可用
char[] buf = new char[(int) f.length()];// 读取的字符数,分配空间
int len = reader.read(buf);// 如果已到达流的末尾,则返回 // -1
// 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
String results = new String(buf, 0, len);
String regex = null;
if (seperators.length > 1) {
regex = "" + seperators[0] + "|" + seperators[1];
} else {
regex = "" + seperators[0];
}
words = results.split(regex);
}
public String nextWord() {
if (pos == words.length)
return null;
return words[pos++];
}
}