Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何提示用户指定写入和读取序列化文件的名称?_Java_Serialization - Fatal编程技术网

Java 如何提示用户指定写入和读取序列化文件的名称?

Java 如何提示用户指定写入和读取序列化文件的名称?,java,serialization,Java,Serialization,我试图提示用户为写入和读取序列化文件创建名称。例如,我可以做以下工作吗: 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

我试图提示用户为写入和读取序列化文件创建名称。例如,我可以做以下工作吗:

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

如果要从终端提示用户输入内容,最简单的方法可能是使用java.io.Console,尤其是其中一种readLine方法:


请参阅。

请将代码分块,以便可读。欢迎使用so!没有运行代码示例的不清楚问题在这里很少受到关注。考虑张贴,包括你试图写文件等。你的意思是命名文件之前,序列化,然后再次去序列化同名????
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