0

I am making an attempt to prompt the user to create the name for the writing and reading serialized file. E.g. could I do the following:

System.out.print("What would you like to name your file?");
String fileName = scanner.nextLine;

try{
 ObjectOutput ostream = new ObjectOutputStream(new FileOutputStream(fileName)) // Would this create a file with the name inputted by the user? If so what would be the extension for the file?

 ostream.writeObject(//whatever I want to put in here)

 //close

//catch

try{

 ObjectInputStream = new ObjectInputStream(new FileInputStream(fileName))

//catch
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • please put your code in blocks so it's readable. – msknapp Sep 22 '14 at 04:14
  • http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console – msknapp Sep 22 '14 at 04:16
  • Welcome to SO! Unclear questions that don't have a running code example get less attention here. Consider posting [SSCCE](http://sscce.org/) including what you're trying to write to file etc. – Nir Alfasi Sep 22 '14 at 04:38
  • Did you mean name the file before serialize it and then again de-serialize with the same name??? – Rockstar Sep 22 '14 at 04:51

1 Answers1

0

If you want to prompt the user for something from the terminal, the easiest way is probably to use java.io.Console, in particular one of its readLine() methods:

import java.io.Console;

...

Console console = System.console();
if (console == null) {
  throw new IllegalStateException("No console to read input from!");
}

String fileName = console.readLine("What would you like to name your file? ");
// Whatever the user inputed is now in fileName

See http://docs.oracle.com/javase/7/docs/api/java/io/Console.html.