我正在尝试读取 .txt 文件并将其附加到 ArrayList。它似乎在 Scanner 行失败并显示“java.io.FileNotFoundException:/raw/words.txt:打开失败:(没有这样的文件或目录)”。我尝试了 BufferReader 方法并更改了文件的位置,但收效甚微。为什么无法识别文件?
public void openFile (){
File file = new File("raw/words.txt");
ArrayList<String> names = new ArrayList<String>();
try{
Scanner in = new Scanner (file);
while (in.hasNextLine()){
names.add(in.nextLine());
}
}
catch (Exception e){
e.printStackTrace();
}
Collections.sort(names);
for(int i=0; i<names.size(); ++i){
System.out.println(names.get(i));
}
}