Java 使用一种方法组合文本文件
大家好,我目前正试图为一项作业编写一个程序,该程序需要4个单独的文本文件,然后使用一种方法,将它们组合成一个。我想知道是否有人能帮我找出这段代码的错误。当我尝试运行它时,我收到一个错误,读取以下内容:Java 使用一种方法组合文本文件,java,methods,io,Java,Methods,Io,大家好,我目前正试图为一项作业编写一个程序,该程序需要4个单独的文本文件,然后使用一种方法,将它们组合成一个。我想知道是否有人能帮我找出这段代码的错误。当我尝试运行它时,我收到一个错误,读取以下内容: "Exception in thread "main" java.io.FileNotFoundException: wonder1.txt (The system cannot find the file specified) at java.io.FileInputStream.ope
"Exception in thread "main" java.io.FileNotFoundException: wonder1.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at asig5.combineFile(asig5.java:26)
at asig5.main(asig5.java:17) "
编写新文件wonder1.txt时,意味着如果项目位置为~/Folder/MyProject,则文件的完整路径为~/Folder/MyProject/wonder1.txt
您需要提供文件的完整路径,或者将文件放在项目文件夹的根目录下。您从刚才发布的日志中了解到了什么?我不明白为什么找不到文件。我正在尝试合并的文件*您确定文件在那里还是要创建它们?文件在我的系统库中。
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
public class asig5 {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter newtext = new PrintWriter("wonder5.txt");
File f0 = new File("wonder1.txt");
File f1 = new File("wonder2.txt");
File f2 = new File("wonder3.txt");
File f3 = new File("wonder4.txt");
combineFile(f0, newtext);
combineFile(f1, newtext);
combineFile(f2, newtext);
combineFile(f3, newtext);
newtext.close();
}
public static void combineFile(File f0, PrintWriter output) throws FileNotFoundException {
Scanner input = new Scanner(f0);
while (input.hasNext()) {
String part1 = input.nextLine();
System.out.println(part1);
output.print(part1);
}
}
}