问题描述
我有一个Java程序,我需要在此程序中要求用户输入其计算机中的csv文件路径,然后该程序应在文件中询问特定的字符串,进行搜索,然后打印出所有行那个字符串。
当我将文件路径硬编码为String path = "C:\Users\Shinky\IdeaProjects\Enumeration\src\Salmon.csv"
之类的字符串时,程序运行良好,但是一旦我手动输入文件路径,我得到:线程“ main”中的异常java.io.FileNotFoundException: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(",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(",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
解决方法
在给定的src文件夹下尝试此操作。
InputStream stream = this.getClass().getResourceAsStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(stream));//BufferedReader
reads csv file