Java FileReader not finding files

后端 未结 2 798
星月不相逢
星月不相逢 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: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();
      }
    }
    

提交回复
热议问题