gpt4 book ai didi

java - 为什么我的程序找不到我的文件?

转载 作者:行者123 更新时间:2023-12-01 10:09:51 28 4
gpt4 key购买 nike

我正在尝试对大文件进行基本扫描,但它不断给我一个 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")这条线是让我痛苦的原因。请帮忙。

最佳答案

  1. 将文件放在项目的根目录下

    即JetBrains Idea 使用项目根目录作为工作路径

  • 使用类路径:

    将文件放置到your_project/main/resources:

  • URL resource = this.getClass().getResource("/yourfile.txt");
    File file = new File(resource.toURI());

    关于java - 为什么我的程序找不到我的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194097/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com