-
prompt for filename
Hi,
I've got a little program that will read back the contents of a dat file, however the only way I can make it work is to hardcode in the filename.
Any suggestions what to change to make it prompt for one?
Code:
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("filename.dat"));
Everything I've tried so far has broken it :confused:
Any suggestions would be great, let me know if you need me to post the rest of the code or not as well. I'm thinking it's only that line that has to be changed though.
Thanks :)
-
Is this just a console program? If it is, you can either take input from the command line or from the console during the program execution. Example:
From Command Line:
Code:
public static void main(String[] argv) {
//argv[0] = first command line parameter (different from C++ style)
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(argv[0]));
//.. other code here...
}
Then you would invoke this with something like:
java ProgramName "Name of file.dat"
Alternatively, you can prompt the user for input with something like.
Code:
public static void main(String[] argv) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("File Name: ");
String fileName = in.readLine();
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filenName));
// Other code here
}
For this code, after you run the class, the program will pause and wait for you to enter input on the console.
Hope this helps.
-
Thank you! :D
That solved it once I worked out the typo in your reply, filenName instead of fileName.
That problem is all solved now, just need to work out the rest.
Thanks
-
I know this is solved but I though I mention you can use the joptionpane class from the swing package. Below is an example.
import java.swing.*;
string input = JOptionPane.showInputDialog(null,"Enter some text");