我正在尝试对大文件进行基本扫描,但是它一直给我一个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();
new FileReader(“LastNames.txt”) 这条线是让我痛苦的原因。请帮助。
答案 0 :(得分:1)
将您的文件置于项目的根目录下
即。 JetBrains Idea使用项目根目录作为工作路径
或
使用classpath:
将您的文件发送到your_project/main/resources
:
URL resource = this.getClass().getResource("/yourfile.txt");
File file = new File(resource.toURI());
答案 1 :(得分:0)
尝试替换
Scanner infile = new Scanner(new FileReader("LastNames.txt"));
带
URL myFile = this.getClass().getResource("LastNames.txt");
Scanner infile = new Scanner(new FileReader(new File(myFile.toURI())));