0

I am trying to write my ArrayList, named Clients, to a file. It works, but unfortunately it just writes symbols into the file and not my actual input.

Below is my code:

    public void SaveToFile() {

    try 
    {
        FileOutputStream fos = new FileOutputStream("CustomerLists.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(Clients);
        oos.close();
    } catch (Exception ex) {
        ex.printStackTrace();
   }

How do I make it work?

thatguy
  • 21,059
  • 6
  • 30
  • 40
Jade
  • 21
  • 3
  • 4
    Do you understand what `writeObject` does? – Makoto Nov 26 '16 at 00:51
  • 1
    It prints in binary format. It's not meant to be human readable. – 4castle Nov 26 '16 at 00:58
  • 1
    Or perhaps more importantly, do you understand what [`ObjectOutputStream`](https://docs.oracle.com/javase/8/docs/api/java/io/ObjectOutputStream.html) does? It creates a **binary** stream of serialized Java objects and values. It you want text, create a [`FileWriter`](https://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html) instead of the `FileOutputStream`, and optionally wrap that in a [`PrintWriter`](https://docs.oracle.com/javase/8/docs/api/java/io/PrintWriter.html) and/or a [`BufferedWriter`](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedWriter.html). – Andreas Nov 26 '16 at 00:58
  • Possible duplicate of [How to write Serialized Object as Human readable Txt file](http://stackoverflow.com/q/16817210/5221149) – Andreas Nov 26 '16 at 01:01
  • The crux of this question is about what `ObjectOutputStream` is for; neither of those possible duplicates really address that. – dimo414 Nov 26 '16 at 01:29
  • 1
    @dimo414 The crux of this question is that the OP is expecting to create a text file, and both duplicates address that precisely. – user207421 Nov 26 '16 at 02:12
  • @EJP well, the goal is to write a text file, but the *confusion* stems from misunderstanding the purpose of `ObjectOutputStream`. Anyways, OP has what they need now. – dimo414 Nov 26 '16 at 02:26
  • @dimo414 thank you for understanding the other question doesn't address what I am looking for at all, – Jade Nov 26 '16 at 11:32
  • @EJP I am dealing with ArrayList here, I know how to write to txt file but my struggle is writing an arraylist to text file – Jade Nov 26 '16 at 11:32
  • @Andreas I changed it the way you told me using Filewriter and BufferedReader but my problem here is you see where I wrote oos.writeobject(Clients) - Clients is my array and when I change it to oos.write it wont allow me because it is a arraylist so what would be an alternative for that – Jade Nov 26 '16 at 11:35

2 Answers2

2

What you've done is serialize a Java object to a file with ObjectOutputStream. That means, Java transforms your object Clients into a specific representation, which covers all the state your object has, e.g. member variables. This representation is not meant to be human-readable, it is a binary format, which is more efficient and suitable for classes and the Java enviroment, than a text format. Look here for more information.

What you can do instead is use a FileWriter, BufferedWriter or PrintWriter, which have multiple write[...](...) methods to write text to a file. In this case, you have to write each variable for its own as String, etc., not the whole class. Here you can an find example on that.

Another option to do the same as ObjectOutputStream for a whole object but in a completely customizable fashion and human-readable form is to use the Java Architecture for XML Binding (JAXB), but this requires a bit of reading. There are also other object serialization libraries for JSON and other formats.

dimo414
  • 47,227
  • 18
  • 148
  • 244
thatguy
  • 21,059
  • 6
  • 30
  • 40
1

Why is it when I run this java program it creates the text file but it prints out symbols instead of my input?

Because it isn't a text file. It is serialized data, written according to the Java Serialization Protocol, because you're using ObjectOutputStream.writeObject().

That also means that CustomerLists.txt is not an appropriate name for this file.

If you want text, use PrintWriter or BufferedWriter.

user207421
  • 305,947
  • 44
  • 307
  • 483