保存到二进制/序列化 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9808539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving to binary/serialization java
提问by Austin
I have to, quote on quote,
我必须在报价上报价,
1.Save accounts to a binary (serialized) file. 2.Load (recreate) accounts from a binary (serialized) file.
1.将帐户保存到二进制(序列化)文件。2.从二进制(序列化)文件加载(重新创建)帐户。
So firstly, I was looking up examples of what exactly that is and I am lost, in same scenarios people mention xml, in my head I think it means like 01010011000 (binary), and when I look at other code it looks like a normal text file save.
所以首先,我正在查找这到底是什么的例子,我迷路了,在同样的情况下人们提到 xml,在我的脑海中,我认为它的意思是 01010011000(二进制),当我查看其他代码时,它看起来像一个正常的文本文件保存。
What exactly does he mean, and can someone post an example, or give me a site that better clarifies this? Once I see what I actually need to do, I can implement it easily, I'm just confused on what exactly is being saved (data-wise) and how.
他到底是什么意思,有人可以发布一个例子,或者给我一个更好地澄清这一点的网站吗?一旦我看到我真正需要做什么,我就可以轻松实现它,我只是对究竟要保存什么(数据方面)以及如何保存感到困惑。
*I already have an option to save via textfile (.txt) if I can just use some of that code for this binary part.
*如果我可以将其中的一些代码用于此二进制部分,我已经可以选择通过文本文件 (.txt) 进行保存。
Thanks!
谢谢!
Here is what I have now, it's still not working I think.
这是我现在所拥有的,我认为它仍然无法正常工作。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SerializationMain implements Serializable {
public static void saveSerialized(Object YourObject, String filePath) throws IOException {
ObjectOutputStream outputStream = null;
try {
outputStream = new ObjectOutputStream(new FileOutputStream(filePath + ".dat"));
outputStream.writeObject(YourObject);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static Object loadSerialized(String filePath, Object[][] data1) throws IOException {
try {
FileInputStream fileIn = new FileInputStream(filePath);
ObjectInputStream in = new ObjectInputStream(fileIn);
try {
data1 = (Object[][]) in.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(SerializationMain.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(data1.length);
return data1;
}
}
采纳答案by Matthew
Assuming you have a class called "account" you simply need to implementsSerializable at the top of your class header.
假设您有一个名为“account”的类,您只需要在类标题的顶部实现Serializable 。
From my understanding, that will serialize all the data into a binary form. You will need to of course still perform the steps to actually write/read the class object out to a file using ObjectOutputStream/ObjectInputStream.
根据我的理解,这会将所有数据序列化为二进制形式。当然,您仍然需要执行使用 ObjectOutputStream/ObjectInputStream 将类对象实际写入/读取到文件的步骤。
So for example...
所以例如...
public class account implements Serializable
{ ...
}
Then in your main function for example, where you want to save the object, you would create a File, attach it to an ObjectOutputStream and write out your object in binary form.
然后在您的主函数中,例如,您要保存对象的位置,您将创建一个文件,将其附加到 ObjectOutputStream 并以二进制形式写出您的对象。
回答by smox
First hit on google: http://www.javacoffeebreak.com/articles/serialization/index.html- basically you should serialize your object to a file. Then you can load it into an object again later.
第一次点击谷歌:http: //www.javacoffeebreak.com/articles/serialization/index.html- 基本上你应该将你的对象序列化到一个文件。然后您可以稍后再次将其加载到对象中。