У меня есть java-программа, в которой мне нужно попросить пользователя ввести путь к файлу csv на своем компьютере, который затем должен запросить определенную строку в файле, найти ее, а затем распечатать все строки с этой строкой .
Программа работает просто отлично, когда я жестко закодирую путь к файлу в строку, например String path = "C:\Users\Shinky\IdeaProjects\Enumeration\src\Salmon.csv"
, но как только я вручную ввожу путь к файлу, я получаю: Exception in thread 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(",");//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