2017-05-04 87 views
0

我收到的时候我想我的LinkedList的内容写入到一个文本文件写作的LinkedList成文本文件,但输出是垃圾

try { 
     FileOutputStream out = new FileOutputStream("ValidMovesMatrix.txt"); 
     ObjectOutputStream oout = new ObjectOutputStream(out); 
     oout.writeObject("--+ This will show all the VALID moves A* algorithm has taken to come up with the solution +---"); 
     oout.writeObject(" "); 
     for (int matrixIdx = 0; matrixIdx < matrixStack.size(); matrixIdx++) { 
      Object matrixShow = matrixStack.get(matrixIdx); 
      oout.writeObject(matrixShow); 
     } 
     oout.close(); 
    } catch (Exception ex) { 
     System.out.println("Output error"); 
    } 

它没有打印我的LinkedList的内容的输出错误和输出文本文件含有如下垃圾:

¬í t _--+ This will show all the VALID moves A* algorithm has taken to come up with the solution +---t {sr java.io.NotSerializableException(Vx ç†5 xr java.io.ObjectStreamExceptiondÃäk9ûß xr java.io.IOExceptionl€sde%ð« xr java.lang.ExceptionÐý>;Ä xr java.lang.ThrowableÕÆ5'9w¸Ë L causet Ljava/lang/Throwable;L 
detailMessaget Ljava/lang/String;[ 
stackTracet [Ljava/lang/StackTraceElement;L suppressedExceptionst Ljava/util/List;xpq ~ t MapStateur [Ljava.lang.StackTraceElement;F*<<ý"9 xp sr java.lang.StackTraceElementa Åš&6Ý… I 
lineNumberL declaringClassq ~ L fileNameq ~ L 
methodNameq ~ xpÿÿÿÿt java.io.ObjectOutputStreampt writeObject0sq ~ 
ÿÿÿÿq ~ pt writeObjectsq ~ 
    bt 
AlgoSolvert AlgoSolver.javat backtrackMovessq ~ 
    'q ~ q ~ t searchsq ~ 
    t 
ProjectOnet ProjectOne.javat 
acceptArgssq ~ 
    q ~ q ~ t mainsr &java.util.Collections$UnmodifiableListü%1µìŽ L listq ~ xr ,java.util.Collections$UnmodifiableCollectionB €Ë^÷ L ct Ljava/util/Collection;xpsr java.util.ArrayListxÒ™Ça I sizexp w xq ~ $x 
+1

你应该让这个类可以序列化,或者自己写一些代码来使它可打印..它实际上转储它的内存。 –

+0

我该怎么做? – wazzza

回答

0

ObjectOutputStream的权利对象作为一个串行化字节代码格式,即可以是稍后重读。如果您想要文本输出,请使用PrintStream或PrintWriter并实现Matrix.toString()以将每个Matrix写为格式文本。

+0

你能告诉我绳子吗? – wazzza

+0

@wazzza你可以在另一个关于toString()方法的QA中阅读'toString()'解释](http://stackoverflow.com/a/3615757/4648586)。 –