我正在开发一个 Java 程序,它逐行读取文本文件,每个文件都有一个数字,将每个数字放入一个数组中,然后尝试使用插入排序对数组进行排序。我需要帮助让程序读取文本文件。
我收到以下错误消息:
java.io.FileNotFoundException: 10_Random (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 insertionSort.main(insertionSort.java:14)
我的“src”“bin”和主项目文件夹中有 .txt 文件的副本,但它仍然找不到该文件。顺便说一句,我正在使用 Eclipse。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class insertionSort {
public static void main(String[] args) {
File file = new File("10_Random");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
int i = sc.nextInt();
System.out.println(i);
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}