-1

I'm following this tutorial on how to output an ArrayList to a text file with ObjectOutputStream, but this removes the line breaks from my original file. In other words, I read a file in, do stuff to it, and write it back out. The data read in remain an ArrayList throughout processing. How do I preserve the line breaks when writing an ArrayList out to file? Do I lose this ability when I read it in as ArrayList?

user207421
  • 305,947
  • 44
  • 307
  • 483
user8121557
  • 149
  • 2
  • 9
  • That tutorial is about how to *serialize* an `ArrayList` to a *binary* file. Binary files don't have lines or line breaks, and neither do `ArrayLists`. Your question doesn't make sense. – user207421 Sep 11 '20 at 00:30

1 Answers1

1

You have misunderstood the tutorial. ObjectOutputStream is not designed for outputting text files. If you try to read a file produced by this class, you will see lots of binary data; i.e. things that show up as unprintable characters or "garbage" if you try to view it as text.

However, ObjectOutputStream it will not be explicitly removing line breaks. It is more likely that the line break characters were removed when you turned your input text into an ArrayList.

But that is moot. If you want to output a text file, don't use ObjectOutputStream. Use a FileWriter and iterate the ArrayList to output each (erm) line followed by a line break.


Do I lose [the line breaks] when I read it in as ArrayList?

That depends on how your read the input file and populated the ArrayList. (We cannot explain the behavior of your code if you don't show it to us. We don't even know for sure what the element type for the ArrayList is ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216