标签:编写 stat [] res import java spl path string
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class A {
public static void main(String[] args) throws Exception {
FileManage a = new FileManage("src/main/resources/a.txt");
FileManage b = new FileManage("src/main/resources/b.txt");
PrintWriter pw=new PrintWriter("src/main/resources/c.txt");
String aWord = null;
String bWord = null;
while((aWord = a.nextWord()) !=null ){
pw.write(aWord + "\n");
bWord = b.nextWord();
if(bWord != null)
pw.write(bWord + "\n");
}
while((bWord = b.nextWord()) != null){
pw.write(bWord + "\n");
} pw.close();
}
}
class FileManage {
String[] arrwords = null;
int pos = 0;
public FileManage(String filepath) throws Exception {
FileInputStream fis = new FileInputStream(filepath);
InputStreamReader in = new InputStreamReader(fis);
BufferedReader read = new BufferedReader(in);
String lines = null;
StringBuffer sb = new StringBuffer();
while ((lines = read.readLine()) != null) {
sb.append(lines).append(" ");
}
arrwords = sb.toString().split(" ");
if(fis!=null){
fis.close();
}
if(in!=null){
in.close();
}
if(read!=null){
read.close();
}
}
public String nextWord() {
if (pos == arrwords.length)
return null;
return arrwords[pos++];
} }
编写一个程序,将 a.txt 文件中的单词与 b.txt 文件中的单词交替合并到 c.txt 文件中,a.txt 文件中的单词用回车符分隔,b.txt 文件中用回车或空格进行分隔。
标签:编写 stat [] res import java spl path string
原文地址:https://www.cnblogs.com/satisfysmy/p/8934457.html