Java 使用FileOutputStream时使用双文件名
我正在尝试使用以下代码保存文件:Java 使用FileOutputStream时使用双文件名,java,file,Java,File,我正在尝试使用以下代码保存文件: //file save first time JFileChooser chooser = new JFileChooser(); //set name for default file for the file chooser. chooser.setSelectedFile(new File(communicator.getFileName())); int retrival = chooser.showSaveDialog(null); if (retr
//file save first time
JFileChooser chooser = new JFileChooser();
//set name for default file for the file chooser.
chooser.setSelectedFile(new File(communicator.getFileName()));
int retrival = chooser.showSaveDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
try {
//write log message to textfield
communicator.writeToField("[Save File]: Saving file at: " + chooser.getSelectedFile().getAbsolutePath());
//setup output stream
FileOutputStream fos = new FileOutputStream(new File(chooser.getSelectedFile().getAbsolutePath()+".bin"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
//get the object to serialize
oos.writeObject(communicator.getCurrentFileObject());
} catch (Exception ex) {
ex.printStackTrace();
}
} else
//write log message to textfield
communicator.writeToField("[Save File]: Operation aborted by user...");
当我使用它时,代码的输出是[保存文件]:将文件保存在:/home/name/documents/test.bin
但每当我去查看文件夹中的实际文件时,它的名称是testtest.bin
。所以这个名字重复了两次。这里有什么问题?什么时候
chooser.getSelectedFile().getAbsolutePath()
已经交付了输出
/home/name/documents/test.bin
正如您的日志所示。为什么还要添加另一个“.bin”
我认为额外的“.bin”导致了您的问题。别提了。日志显示,您不需要它。您报告的内容没有意义-如果消息是“将文件保存在:/home/name/documents/test.bin”,并且实际文件名为“test.bin.bin”,那么如果您没有在消息中添加“.bin”,那么这将是有意义的,但是你在
文件中
构造函数中……是的,但这并没有发生。这有助于找到解决方案,但这不是问题本身。我查看了整个程序,发现有一个地方可以将名称添加到路径中,但它已经准备好了,,,
new File(chooser.getSelectedFile().getAbsolutePath()+".bin")