Java FileReader not finding files

陌路散爱 提交于 2019-12-01 14:51:21

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.

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();
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!