java.io.FileNotFoundException: (系统找不到指定的文件) 在请求文件路径时发生异常

huangapple go评论76阅读模式
英文:

Exception in thread "main" java.io.FileNotFoundException: (The system cannot find the file specified) when asking for file path

问题

import java.io.*;

public class StageReport {

    private static StageReport run;         
    private static FileInputStream path;    
    private Stage stage;                    
    private static String choice;           
    private static String file;             

    public StageReport(Stage stage){
        this.stage = stage;
    }

    public void reader() throws Exception{
        String line;
        BufferedReader br = new BufferedReader(new FileReader(String.valueOf(path)));
        
        switch (stage) {
            case FISH:
                while ((line = br.readLine()) != null) {
                    String[] values = line.split(",");
                    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(",");
                    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(",");
                    if(values[1].equalsIgnoreCase("Harvesting")){
                        System.out.println(values[0] + " " + values[1] + " " + values[2]);
                    }
                }
                break;
            default:
                System.out.println("No valid entries found");
                break;
        }
    }

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        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();

        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();
    }
}

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

    private String name;        

    Stage(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}
英文:

I have a java program where I need to ask a user for an input of a csv file path in their computer, which it should then ask for a specific string within the file, search for that, and then print out all rows with that string.

The program works just fine when I hardcode the file path into a String like String path = "C:\Users\Shinky\IdeaProjects\Enumeration\src\Salmon.csv", but once I manually enter the file path I get: Exception in thread "main" java.io.FileNotFoundException: java.io.FileInputStream@1b6d3586 (The system cannot find the file specified)

I've tried storing the file directly underneath my project and also in the src folder, but I get the same Error every time. At this point I don't know what to do or if it would be easier to ask for a file path outside my IDE in general, any help would be greatly appreciated.

Code:

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

得分: 1

尝试一下,给定位于 src 文件夹下的文件。

InputStream stream = this.getClass().getResourceAsStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(stream));//BufferedReader 读取 csv 文件
英文:

Try this, given file under src folder.

 InputStream stream = this.getClass().getResourceAsStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(stream));//BufferedReader 
reads csv file

huangapple
  • 本文由 发表于 2020年10月13日 09:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64327295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定