Java FileReader not finding files

后端 未结 2 799
星月不相逢
星月不相逢 2021-01-17 06:20

I decided to start a new question so it can strictly focus on the FileReader errors.

This is a method that takes in a file name, and a desired output name for a new

相关标签:
2条回答
  • 2021-01-17 06:55

    Print out what current_directory is and confirm that it matches your expectations.

    I'd also print out the complete file path that you pass to the FileReader as well.

    Most times that behavior doesn't match my expectations, I find that my assumptions were wrong.

    0 讨论(0)
  • 2021-01-17 06:57

    No such file or directory would normally mean file does not exist. Please edit the code and debug it either using a IDE or by adding print statements. Also, current_directory could be renamed currentDirectory to be consistent with Java naming conventions. Try running code below.

    public static void fileGenerator(String in, String out) {      
    try {
        String currentDirectory = System.getProperty("user.dir");
        System.out.println(currentDirectory);
        String inputFileName = currentDirectory+"/"+in;
        File inputFile = new File(inputFileName);
        System.out.println(inputFile.getAbsolutePath());
        FileReader inputFileReader = new FileReader(inputFile);
        Scanner input = new Scanner(inputFileReader);
        PrintWriter output = new PrintWriter(currentDirectory+"/"+out);
        while(input.hasNext()) {
            String line = input.nextLine(); 
        output.println(line);
        output.close(); 
        }
      }  catch (Exception e) { 
          e.printStackTrace();
      }
    }
    
    0 讨论(0)
提交回复
热议问题