我正在尝试对大文件进行基本扫描,但是它一直给我一个FileNotFound异常,但是该文件与类位于同一文件夹中。(当前使用mac)
import java.io.*;
import java.util.Scanner;
public class LastNameSearch {
static PopularName[] people= new PopularName[151671];
public static void main(String[] args){
String nextString=null;
PopularName nextName;
String[] info=new String[5];
Scanner infile = new Scanner(new FileReader("LastNames.txt"));
int index=0;
while(infile.hasNext()){
nextString=infile.nextLine();
info=nextString.split(",");
nextName=new PopularName(info[0], info[1], info[2], info[3], info[4]);
people[index]=nextName;
}
infile.close();
新的FileReader(“ LastNames.txt”)
这条线使我感到痛苦。请帮忙。
最佳答案
将文件放在项目的根目录下
即JetBrains Idea使用项目根目录作为工作路径
要么
使用类路径:
将文件放置到your_project/main/resources
:
URL resource = this.getClass().getResource("/yourfile.txt");
File file = new File(resource.toURI());