将LinkedList写入文本文件但输出是垃圾

时间:2017-05-04 09:12:11

标签: java fileoutputstream objectoutputstream

当我尝试将我的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");
    }

它没有打印我的链表的内容,输出文本文件包含以下垃圾:

¬í 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 :(得分:0)

ObjectOutputStream将对象作为序列化字节码格式,以后可以重读。如果您想要文本输出,请使用PrintStream或PrintWriter并实现Matrix.toString()将每个Matrix写为格式化文本。