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

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

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

问题

  1. import java.io.*;
  2. public class StageReport {
  3. private static StageReport run;
  4. private static FileInputStream path;
  5. private Stage stage;
  6. private static String choice;
  7. private static String file;
  8. public StageReport(Stage stage){
  9. this.stage = stage;
  10. }
  11. public void reader() throws Exception{
  12. String line;
  13. BufferedReader br = new BufferedReader(new FileReader(String.valueOf(path)));
  14. switch (stage) {
  15. case FISH:
  16. while ((line = br.readLine()) != null) {
  17. String[] values = line.split(",");
  18. if(values[1].equalsIgnoreCase("Fish Entry")){
  19. System.out.println(values[0] + " " + values[1] + " " + values[2]);
  20. }
  21. }
  22. break;
  23. case GROW:
  24. while ((line = br.readLine()) != null) {
  25. String[] values = line.split(",");
  26. if(values[1].equalsIgnoreCase("Grow-out")){
  27. System.out.println(values[0] + " " + values[1] + " " + values[2]);
  28. }
  29. }
  30. break;
  31. case HARVESTING:
  32. while ((line = br.readLine()) != null) {
  33. String[] values = line.split(",");
  34. if(values[1].equalsIgnoreCase("Harvesting")){
  35. System.out.println(values[0] + " " + values[1] + " " + values[2]);
  36. }
  37. }
  38. break;
  39. default:
  40. System.out.println("No valid entries found");
  41. break;
  42. }
  43. }
  44. public static void main(String[] args) throws Exception{
  45. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  46. System.out.print("Enter input file path and name: ");
  47. file = br.readLine();
  48. System.out.println("You entered: " + file);
  49. path = new FileInputStream(file);
  50. System.out.print("Enter the stage you would like to see: ");
  51. choice = br.readLine();
  52. if (choice.equalsIgnoreCase(String.valueOf(Stage.FISH))) {
  53. run = new StageReport(Stage.FISH);
  54. run.reader();
  55. } else if (choice.equalsIgnoreCase(String.valueOf(Stage.GROW))) {
  56. run = new StageReport(Stage.GROW);
  57. run.reader();
  58. } else
  59. run = new StageReport(Stage.HARVESTING);
  60. run.reader();
  61. }
  62. }
  63. enum Stage{ FISH("Fish Entry"), GROW("Grow-out"), HARVESTING("Harvesting");
  64. private String name;
  65. Stage(String name) {
  66. this.name = name;
  67. }
  68. @Override
  69. public String toString() {
  70. return name;
  71. }
  72. }
英文:

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:

  1. import java.io.*;
  2. public class StageReport {
  3. private static StageReport run; //StageReport object
  4. private static FileInputStream path; //Object path for csv file
  5. private Stage stage; //Object stage
  6. private static String choice; //String choice for input
  7. private static String file; //String inFile for csv file path input
  8. /**
  9. * constructor
  10. */
  11. public StageReport(Stage stage){
  12. this.stage = stage;
  13. }//end StageReport
  14. /**
  15. * finds and outputs correct mortality rates of fish
  16. */
  17. public void reader() throws Exception{
  18. //String path = "C:\\Users\\Shinky\\Desktop\\Salmon.csv";
  19. String line;
  20. BufferedReader br = new BufferedReader(new FileReader(String.valueOf(path)));//BufferedReader reads csv file
  21. //switch for enum Stage class
  22. switch (stage) {
  23. case FISH:
  24. while ((line = br.readLine()) != null) {
  25. String[] values = line.split(",");//csv file lines split from ,
  26. //searches through file to find stages that correspond with user input and outputs correct year, stage and mortality rate
  27. if(values[1].equalsIgnoreCase("Fish Entry")){
  28. System.out.println(values[0]+" "+values[1]+" "+values[2]);
  29. }
  30. }
  31. break;
  32. case GROW:
  33. while ((line = br.readLine()) != null) {
  34. String[] values = line.split(",");//csv file lines split from ,
  35. //searches through file to find stages that correspond with user input and outputs correct year, stage and mortality rate
  36. if(values[1].equalsIgnoreCase("Grow-out")){
  37. System.out.println(values[0]+" "+values[1]+" "+values[2]);
  38. }
  39. }
  40. break;
  41. case HARVESTING:
  42. while ((line = br.readLine()) != null) {
  43. String[] values = line.split(",");//csv file lines split from ,
  44. //searches through file to find stages that correspond with user input and outputs correct year, stage and mortality rate
  45. if(values[1].equalsIgnoreCase("Harvesting")){
  46. System.out.println(values[0]+" "+values[1]+" "+values[2]);
  47. }
  48. }
  49. break;
  50. default:
  51. System.out.println("No valid entries found");//prompt for no entries found in csv file
  52. break;
  53. }
  54. }//end reader
  55. /**
  56. * constructor
  57. */
  58. public static void main(String[] args) throws Exception{
  59. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//BufferedReader declared
  60. System.out.print("Enter input file path and name: ");
  61. file = br.readLine();
  62. System.out.println("You entered: " + file);
  63. path = new FileInputStream(file);
  64. System.out.print("Enter the stage you would like to see: ");
  65. choice = br.readLine();
  66. //checks if user input matches enum Stage strings
  67. if (choice.equalsIgnoreCase(String.valueOf(Stage.FISH))) {
  68. run = new StageReport(Stage.FISH);
  69. run.reader();
  70. } else if (choice.equalsIgnoreCase(String.valueOf(Stage.GROW))) {
  71. run = new StageReport(Stage.GROW);
  72. run.reader();
  73. } else
  74. run = new StageReport(Stage.HARVESTING);
  75. run.reader();
  76. }//end main
  77. }//end StageReport
  78. enum Stage{ FISH("Fish Entry"), GROW("Grow-out"), HARVESTING("Harvesting");
  79. private String name; //String name for enum names
  80. /**
  81. * Sets String names
  82. * @param name the names of the Strings in the enum classs
  83. */
  84. Stage(String name) {
  85. this.name = name;
  86. }//end Stage
  87. @Override
  88. public String toString() {
  89. return name;
  90. }//end String
  91. }//end Stage

答案1

得分: 1

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

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

Try this, given file under src folder.

  1. InputStream stream = this.getClass().getResourceAsStream(file);
  2. BufferedReader br = new BufferedReader(new InputStreamReader(stream));//BufferedReader
  3. 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:

确定