0

I am getting an output error when I am trying to write the contents of my Linkedlist into a text file

    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");
    }

It didn't print the contents of my linkedlist and the output text file contained the following garbage:

¬í 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
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

1 Answers1

2

ObjectOutputStream write objects as a serialized byte code format, that can be later reread.

If you want a text output use a PrintStream or PrintWriter and implement Matrix.toString() to write each Matrix as formated text.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
Dr BDO Adams
  • 416
  • 1
  • 5
  • 13
  • @wazzza you can read `toString()` explanation in [another QA about `toString()` method](http://stackoverflow.com/a/3615757/4648586). – Bagus Tesa May 04 '17 at 09:40