I'm trying to write a binary file to a specified folder, however it keeps giving me an exception. For example, if I write the file without specifying any folder the program writes it with no problem:
public void saveFile(String name) throws IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(name + ".bin"));
out.writeObject(this);
out.close();
}
However, when I try to specify the folder the program just doesn't write the file:
public void saveFile(String name) throws IOException {
File location = new File("/path/" + name + ".bin");
FileOutputStream fos = new FileOutputStream(location);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(this);
out.close();
fos.close();
}
I tryed several different ways but still no solution. Does anybody know what am I doing wrong?
Check if the class which you want to write is Serializable
or not.
public class Foo implements java.io.Serializable{
//...
public void write() throws IOException{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Test.bin"));
os.writeObject(this);
os.close();
}
}
Another problem: If there is no folder named path
it cannot write the object
Check your code again.
似乎非序列化的唯一原因是,您可能尚未实现Serializable接口,并且无法正确输入路径名称,例如:-“ C:\\ Users \\ ..”希望它能正常工作
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.