I have the following code:
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter: ");
String m = inFromUser.readLine();
BufferedReader br = new BufferedReader(new FileReader(m));
String text;
while((text = br.readLine()) != null){
System.out.println(text);
}
br.close();
And when reading in the string from the user the file can never be found. I don’t know what is happening when the string is passed but it becomes unfindable. Although the file is very much there.
I have run tests and when I replace the newFileReader(m) with newFileReader(“Lab3/test”) the file is found, but when I enter the “Lab3/test” into the system(Via the String m = inFromUser.readLine();) to read it, it’s read in and printed as “Lab3/test” but the file reader never finds the file when the string is passed into the file reader(Via m), when the string is the exact same.
I’ve googled a bit but trying to explain that is a pain.
A thank you in advance, Sam
You need to enter the file name without quotes. If you enter it with quotes, then the file name must contain the quotes as well.
If you use file name such as
Lab3/test, then Java will search for the file in the current working directory. The current working directory is normally where the application was started. I might be easier if you enter the complete, absolute file name, such asC:\Users\Smithers\Dropbox\Software Engineering Code\Eclipse\Networking\Lab3\test.It doesn’t matter if you use backslashes or forward slashes.