我正在开发一个Android项目,我正在尝试使用objectOutputStream将Trie保存到文件中,然后能够重新读取它。由于某种原因,我不断得到StackOverFlowError,即使在我的特里有三个字。
尝试代码:http://algs4.cs.princeton.edu/52trie/TST.java.html
这是我的读/写代码
public static void writeObject(Context c, String key, Object object)
{
try{
FileOutputStream fileOutputStream = c.openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.close();
fileOutputStream.close();
} catch(IOException e) {}
}
public static Object readObject(Context c, String key)
{
Object object = null;
try{
FileInputStream fileInputStream = c.openFileInput(key);
try{
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
try{
object = objectInputStream.readObject();}
catch (ClassNotFoundException e) {}
;} catch (IOException e) {}
} catch (FileNotFoundException e) {}
return object;
}
这是我得到的错误。 3字Trie不应该导致堆栈溢出错误。
java.lang.StackOverflowError
at java.lang.Thread.currentThread(Thread.java:476)
at java.lang.ThreadLocal.get(ThreadLocal.java:53)
at dalvik.system.BlockGuard.getThreadPolicy(BlockGuard.java:140)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:148)
at libcore.io.IoBridge.read(IoBridge.java:422)
at java.io.FileInputStream.read(FileInputStream.java:179)
at libcore.io.Streams.readSingleByte(Streams.java:41)
at java.io.FileInputStream.read(FileInputStream.java:175)
at java.io.DataInputStream.readByte(DataInputStream.java:96)
at java.io.ObjectInputStream.nextTC(ObjectInputStream.java:506)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:680)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1799)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1851)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1851)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1851)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1851)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1369)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1851)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1999)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1956)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1137)
at java