September 23rd, 2012, 03:05 PM
File reader based on user input
Yesterday I was playing around with file readers, trying to get things to work. I managed to get things working but I have a question. It was only possible to read from a file when I specified what the file name was in the code. What I'd like to do is prompt the user for the file name to output.
How do I do this?
September 23rd, 2012, 03:18 PM
Look at the JFileChooser class. It presents a prompt that the user can fill in.
September 23rd, 2012, 04:07 PM
I actually was experimenting and did it a different way. I need to stop creating threads before trying on my own.
Originally Posted by NormR
This is what I did.
System.out.println("Please input the name of the file");
Scanner file = new Scanner (System.in);
String filename = file.next();
Scanner in = new Scanner(new FileReader(filename));
September 23rd, 2012, 04:14 PM
Having the user enter some text that the code uses as a path to a file can be a problem if the user doesn't spell the path correctly.
Reading user input into a String is a lot easier for you to code.