当我尝试将两个 (.text) 文件合并到另一个 (.text) 文件时,为什么我的代码不起作用?
Why does my code not work when I try to merge two (.txt) files into another (.txt) file?
我有两个 .txt 文件(file1.txt 和 file2.txt)。在这些文件中有一些字符行。我的目的是将这两个文件的内容合并到另一个文件中(file3.txt)。我的代码如下:
public static void main(String[] args) {
try {
PrintWriter pw = new PrintWriter("file3.txt");
BufferedReader br1 = new BufferedReader(new FileReader("file1.txt"));
BufferedReader br2 = new BufferedReader(new FileReader("file2.txt"));
String line = br1.readLine();
while(line!=null){
pw.println(line);
br1.readLine();
}
line = br2.readLine();
while (line!=null) {
pw.println(line);
br2.readLine();
}
pw.flush();
pw.close();
br1.close();
br2.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaIoProject.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JavaIoProject.class.getName()).log(Level.SEVERE, null, ex);
}
}
编译时没有报错。在 运行 之后,当我尝试在 (file3.txt) 中查看预期输出时,它没有显示任何内容,并且鼠标指针变为正在处理。为什么会这样。我忘记添加的缺失部分在哪里或我应该编辑哪一部分以及为什么..需要你的帮助..谢谢.
您错过了在循环中重新分配 line 的值,因此您得到了一个无限循环。
更改两个 while 循环:
while (line!=null) {
pw.println(line);
line =br2.readLine();
}
您错过了作业。所以你可以尝试这样的事情。
String line ="";
while((line=br1.readLine())!=null){
pw.println(line);
}
line = "";
while ((line=br2.readLine())!=null) {
pw.println(line);
}
Lots of code you have repeated multiple times in your implementation. You can
simply create a method and invoke it as per filename.
PrintWriter pw = new PrintWriter("file3.txt");
readAndWrite(pw, "file1.txt");
readAndWrite(pw, "file2.txt");
pw.flush();
pw.close();
这是 readAndWrite 方法的定义。还要更正循环。
private static void readAndWrite(PrintWriter pw, String filename) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = br.readLine();
while (line!=null) {
pw.println(line);
line =br.readLine();
}
br.close();
}
我有两个 .txt 文件(file1.txt 和 file2.txt)。在这些文件中有一些字符行。我的目的是将这两个文件的内容合并到另一个文件中(file3.txt)。我的代码如下:
public static void main(String[] args) {
try {
PrintWriter pw = new PrintWriter("file3.txt");
BufferedReader br1 = new BufferedReader(new FileReader("file1.txt"));
BufferedReader br2 = new BufferedReader(new FileReader("file2.txt"));
String line = br1.readLine();
while(line!=null){
pw.println(line);
br1.readLine();
}
line = br2.readLine();
while (line!=null) {
pw.println(line);
br2.readLine();
}
pw.flush();
pw.close();
br1.close();
br2.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaIoProject.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JavaIoProject.class.getName()).log(Level.SEVERE, null, ex);
}
}
编译时没有报错。在 运行 之后,当我尝试在 (file3.txt) 中查看预期输出时,它没有显示任何内容,并且鼠标指针变为正在处理。为什么会这样。我忘记添加的缺失部分在哪里或我应该编辑哪一部分以及为什么..需要你的帮助..谢谢.
您错过了在循环中重新分配 line 的值,因此您得到了一个无限循环。
更改两个 while 循环:
while (line!=null) {
pw.println(line);
line =br2.readLine();
}
您错过了作业。所以你可以尝试这样的事情。
String line ="";
while((line=br1.readLine())!=null){
pw.println(line);
}
line = "";
while ((line=br2.readLine())!=null) {
pw.println(line);
}
Lots of code you have repeated multiple times in your implementation. You can simply create a method and invoke it as per filename.
PrintWriter pw = new PrintWriter("file3.txt");
readAndWrite(pw, "file1.txt");
readAndWrite(pw, "file2.txt");
pw.flush();
pw.close();
这是 readAndWrite 方法的定义。还要更正循环。
private static void readAndWrite(PrintWriter pw, String filename) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = br.readLine();
while (line!=null) {
pw.println(line);
line =br.readLine();
}
br.close();
}