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
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();
}
}