【发布时间】:2020-10-13 01:36:12
【问题描述】:
我有一个 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(",");//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
【问题讨论】:
-
尝试使用正向 slshes
-
红旗似乎是
String.valueOf(path)- 是path还不是String? -
您正在尝试使用 FileInputStream 作为文件名。查看您的代码——有效的路径是一个字符串,现在您已将路径更改为 FileInputStream。删除 String.valueOf() 并将路径传递给 FileReader 构造函数。
-
将
FileInputStream path更改为File path,将String.valueOf(path)更改为仅path并将path = new FileInputStream(file)更改为path = new File(file)inmain(String[] args) -
文件代表文件系统中的一个命名位置,它可能存在也可能不存在。 FileInputStream 是您在读取文件中包含的数据时收到的字节序列(显然必须实际存在)。
标签: java csv file input filestream