当前位置: 首页 > 知识库问答 >
问题:

线程“main”java中出现异常。木卫一。FileNotFoundException:(系统在请求文件路径时找不到指定的文件)

辛龙野
2023-03-14

我有一个java程序,我需要要求用户在他们的计算机中输入一个csv文件路径,然后它应该要求在文件中输入一个特定的字符串,搜索该字符串,然后打印出带有该字符串的所有行。

当我把文件路径硬编码成字符串时,这个程序工作得很好,比如String path="C:\用户\Shinky\ideaProjects\枚举\src\Salmon.csv",但是一旦我手动输入文件路径,我就会得到:线程“main”中的异常java.io.FileNotFoundExc0019:java.io.FileInputStream@1b6d3586(系统找不到指定的文件)

我尝试将文件直接存储在我的项目下面,也存储在src文件夹中,但每次都会出现相同的错误。在这一点上,我不知道该做什么,或者如果在我的IDE之外请求一个文件路径通常会更容易,任何帮助都将不胜感激。

代码:

import java.io.*;

public class StageReport {

    private static StageReport run;         //StageReport object
    private static FileInputStream path;    //Object path for csv file
    private Stage stage;                    //Object stage
    private static String choice;           //String choice for input
    private static String file;             //String inFile for csv file path input

    /**
     * constructor
     */
    public StageReport(Stage stage){
        this.stage = stage;
    }//end StageReport


    /**
     * finds and outputs correct mortality rates of fish
     */
    public void reader() throws Exception{

        //String path = "C:\\Users\\Shinky\\Desktop\\Salmon.csv";
        String line;

        BufferedReader br = new BufferedReader(new FileReader(String.valueOf(path)));//BufferedReader reads csv file

        //switch for enum Stage class
        switch (stage) {
            case FISH:
                while ((line = br.readLine()) != null) {
                    String[] values = line.split(",");//csv file lines split from ,
                    //searches through file to find stages that correspond with user input and outputs correct year, stage and mortality rate
                    if(values[1].equalsIgnoreCase("Fish Entry")){
                        System.out.println(values[0]+" "+values[1]+" "+values[2]);
                    }

                }
                break;

            case GROW:
                while ((line = br.readLine()) != null) {
                    String[] values = line.split(",");//csv file lines split from ,
                    //searches through file to find stages that correspond with user input and outputs correct year, stage and mortality rate
                    if(values[1].equalsIgnoreCase("Grow-out")){
                        System.out.println(values[0]+" "+values[1]+" "+values[2]);
                    }

                }
                break;

            case HARVESTING:
                while ((line = br.readLine()) != null) {
                    String[] values = line.split(",");//csv file lines split from ,
                    //searches through file to find stages that correspond with user input and outputs correct year, stage and mortality rate
                    if(values[1].equalsIgnoreCase("Harvesting")){
                        System.out.println(values[0]+" "+values[1]+" "+values[2]);
                    }

                }

                break;

            default:
                System.out.println("No valid entries found");//prompt for no entries found in csv file
                break;
        }

    }//end reader

    /**
     * constructor
     */
    public static void main(String[] args) throws Exception{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//BufferedReader declared
        System.out.print("Enter input file path and name: ");
        file = br.readLine();
        System.out.println("You entered: " + file);
        path = new FileInputStream(file);

        System.out.print("Enter the stage you would like to see: ");
        choice = br.readLine();


        //checks if user input matches enum Stage strings
        if (choice.equalsIgnoreCase(String.valueOf(Stage.FISH))) {
            run = new StageReport(Stage.FISH);
            run.reader();
        } else if (choice.equalsIgnoreCase(String.valueOf(Stage.GROW))) {
            run = new StageReport(Stage.GROW);
            run.reader();
        } else
            run = new StageReport(Stage.HARVESTING);
        run.reader();

    }//end main

}//end StageReport

enum Stage{ FISH("Fish Entry"), GROW("Grow-out"), HARVESTING("Harvesting");

    private String name;        //String name for enum names


    /**
     * Sets String names
     * @param name the names of the Strings in the enum classs
     */
    Stage(String name) {
        this.name = name;
    }//end Stage

    @Override
    public String toString() {
        return name;
    }//end String

}//end Stage

共有1个答案

盛骏祥
2023-03-14

试试这个,src文件夹下的给定文件。

 InputStream stream = this.getClass().getResourceAsStream(file);
 BufferedReader br = new BufferedReader(new InputStreamReader(stream));//BufferedReader 
 reads csv file
 类似资料:
  • 我有一个名为""的文件。 它与我的< code>java文件在同一个目录中。 但是,当我尝试在以下代码中访问它时,会发生此文件未找到错误: 这是我的代码:

  • 问题内容: 我正在制作一个打开并读取文件的程序。这是我的代码: 但是当我跑步时,出现以下错误: 我和位于同一目录中的以下位置: 我想知道我在做什么错? 问题答案: 我已经复制了您的代码,并且运行正常。 我怀疑您在hello.txt的实际文件名中仅存在一些问题,或者您在错误的目录中运行。考虑通过@ Eng.Fouad建议的方法进行验证

  • 在我的servlet中,我写了以下语句来加载本体论文件 但是当我运行这个项目时,我得到了以下例外 org.semanticweb.owlapi.io.OWLOntologyInputSourceExc0019:java.io.FileNotFoundExc0019: src\java\本体论\ontology.owl(系统找不到指定的路径) 我怎样才能克服这个问题?

  • 我正在运行一个cron调度程序,它将运行查询并导出到资源文件夹中的excel文件中。当我在IDE中运行程序时,它工作正常,文件在资源下创建,但是当我在Tomcat中部署代码时,收到此错误“java.io.FileNotFoundException:src\main\resources\报告\测试查询.xlsx(系统找不到指定的路径)” IDE输出:启动已成功关闭的已连接执行导出 Tomcat的输出

  • 我得到以下Tomcat错误: 发布到Tomcat v8.5 Server atlocalhost...遇到问题。发布失败并出现多个错误。 细节: 发布失败,出现多个错误 找不到文件:C:\Users…\HerramentationLogica\target\m2e wtp\web资源\META-INF\MANIFEST.MF。 未找到文件:C:\Users…\HerramentationLogic

  • 我已经编写了一个基本的jsp代码,用于存储和检索数据库中的数据。在此之前,我正在检查用户验证。 当我点击提交按钮时,它将重定向到我的jsp页面。 我已经写了一个数据库。属性文件分开。 当我给出了读取属性文件的完整路径时。,程序执行正常。(这不是像下面这样硬代码的最佳方式)。 FileInputStream in=new FileInputStream("C:\Program Files\Apach