2017-05-04 50 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)。 –