writeObject(此)无法写入对象java
我正在使用ObjectOutputStream保存对象,但当我使用.writeObject(this)将其保存为文件时,无法保存材料。我定义的类已可序列化writeObject(此)无法写入对象java,java,objectoutputstream,Java,Objectoutputstream,我正在使用ObjectOutputStream保存对象,但当我使用.writeObject(this)将其保存为文件时,无法保存材料。我定义的类已可序列化 public class LanguageModel implements Serializable { private static LanguageModel lm_; /* ******************************* */ //word -> count(w) public static Dictiona
public class LanguageModel implements Serializable {
private static LanguageModel lm_;
/* ******************************* */
//word -> count(w)
public static Dictionary unigramDict = new Dictionary();
//word_pair -> count(wi,wi+1)
public static Dictionary bigramDict = new Dictionary();
private static int wordIdCounter = 0;
/* ***************************** */
// Do not call constructor directly since this is a Singleton
private LanguageModel(String corpusFilePath) throws Exception {
constructDictionaries(corpusFilePath);
}
public void constructDictionaries(String corpusFilePath)
throws Exception {
...
}
// Saves the object (and all associated data) to disk
public void save() throws Exception{
FileOutputStream saveFile = new FileOutputStream(Config.languageModelFile);
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(this);
save.close();
}
// Creates a new lm object from a corpus
public static LanguageModel create(String corpusFilePath) throws Exception {
if(lm_ == null ){
lm_ = new LanguageModel(corpusFilePath);
}
return lm_;
}
}
I类定义如下:
import java.io.Serializable;
import java.util.HashMap;
public class Dictionary implements Serializable {
private int termCount;
private HashMap<String, Integer> map;
public int termCount() {
return termCount;
}
public Dictionary() {
termCount = 0;
map = new HashMap<String, Integer>();
}
...
}
import java.io.Serializable;
导入java.util.HashMap;
公共类字典实现可序列化{
私人国际术语计数;
私有哈希映射;
公共int termCount(){
返回项计数;
}
公共词典(){
termCount=0;
map=新的HashMap();
}
...
}
当我尝试
save.writeObject(unigramDict)
时,它可以正确地保存此变量。因为它是一个大变量,所以我可以简单地检查文件的大小。它是5MB。当我切换到save.writeObject(this)
时,文件的大小只有53字节。我想您遇到了静态字段的问题,这些字段不能用save.writeObject(this)
保存
从javadoc:
对象的默认序列化机制写入
对象、类签名和所有非瞬态
和非静态字段
您只需将unigramDict
和bigramDict
设置为非静态字段,并使用languageModel.lm\uuu.unigramDict
访问它也许您可以查看,而不是将所有字段设置为
static
我认为您在使用save.writeObject(this)
保存静态字段时遇到了麻烦
从javadoc:
对象的默认序列化机制写入
对象、类签名和所有非瞬态
和非静态字段
您只需将unigramDict
和bigramDict
设置为非静态字段,并使用languageModel.lm\uuu.unigramDict
访问它也许你可以看看,而不是把所有字段都设置为
静态我不理解最后一段。你说save.writeObject(unigramDict)
给你5MB,然后你说save.writeObject(unigramDict)
给你53B。是哪一个?顺便说一下,在保存之前,您可以使用调试器检查字典
对象是否已正确填充。@DavidWallace我认为第二个unigramDict
可能是此
,在第一种情况下,他保存了映射
,在第二种情况下,他保存LanguageModel
对象,而不保存静态字段:)Java序列化只保存非瞬态和非静态字段。这是因为它试图保存该实例的字段。使任何要保存的字段成为非静态的。哦,我没有看到静态的
修饰符。哎呀,我不懂最后一段。你说save.writeObject(unigramDict)
给你5MB,然后你说save.writeObject(unigramDict)
给你53B。是哪一个?顺便说一下,在保存之前,您可以使用调试器检查字典
对象是否已正确填充。@DavidWallace我认为第二个unigramDict
可能是此
,在第一种情况下,他保存了映射
,在第二种情况下,他保存LanguageModel
对象,而不保存静态字段:)Java序列化只保存非瞬态和非静态字段。这是因为它试图保存该实例的字段。使任何要保存的字段成为非静态的。哦,我没有看到静态的
修饰符。哎呀。