1

I wrote a code to save my ArrayList to a file, but I need to read this file again, reuse the same array later, but the file is being writen in a weird way, is there any way I can configure the way that the output file will be writen? Sorry if the question is stupid, this is my first program.

Code to save the ArrayList:

try {
      FileOutputStream fileOut = new FileOutputStream(
          "src//ServerInfo.txt");
      ObjectOutputStream out = new ObjectOutputStream(fileOut);
      out.writeObject(dataServer);
      out.close();
      fileOut.close();
} 

      catch (IOException i) {
      i.printStackTrace();
}

Code to read the file:

try {
    File file = new File("src//ServerInfo.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    try {
        String s;
        while ((s = br.readLine()) != null) {
            dataServer.add(s);
        }
    } 
    finally {
        br.close();
    }
} 

catch (IOException ex) {
    ex.printStackTrace();
}

Either changing the read or write code is okay, I just need a way to read the file I write.

Output file llok like this:

¬í sr java.util.ArrayListxÒ™Ça I sizexp   w   t admint admint booklet@booklet.comt 
Administratorx

How it's supposed to look like: (it's also how I wrote it the first time, before the program did it)

admin
admin
booklet@booklet.com
Administrator
Giovanni Marcolla
  • 103
  • 1
  • 1
  • 4
  • 2
    possible duplicate of [Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?](http://stackoverflow.com/questions/16111496/java-how-can-i-write-my-arraylist-to-a-file-and-read-load-that-file-to-the) – vefthym Nov 06 '14 at 18:04
  • Yeah, you're using default object serialization, which is why you're getting the horrendous format. Either follow the advise given on the linked duplicate question from @vefthym OR you could always convert it to a JSON string using any of the good JSON libraries. This is basically a form of serialization, but JSON is widely used and supported in case you ever want the file to be passed around for other uses. – Dan Temple Nov 06 '14 at 18:07
  • Make sure you're using @Amir Kost's answer from the linked duplicate rather than the accepted answer (which will work, but fails to utilize the advantage of serialization). – sage88 Nov 06 '14 at 18:10
  • 1
    Not strictly a duplicate. The OP probably needs an explanation of what an object stream is in addition to how to write the file correctly. – Mad Physicist Nov 06 '14 at 18:16

3 Answers3

1

While your reading code is reasonable for the format you want, you are writing the file in a different format. An object stream is a biary stream that is part of Java's default serialization mechanism, as pointed out by Dan Temple. The serialized object will include things like the class type and the serial version in addition to the contents of the object. Having objects other than strings would make the output even more complicated.

If you want to have a pure textual representation of your list, do something like this:

public void save(String fileName) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
    for (String element : dataServer)
        pw.println(element);
    pw.close();
}

This is a nearly verbatim copy-and-paste from the answer to Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?, as mentioned by vefthym. You may want to add your own exception handling to it, as you did with the reading code.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • May I ask you what "Club club : clubs" stands for in the code? is it my array name? – Giovanni Marcolla Nov 07 '14 at 01:46
  • Sorry, that was a careless copy-and-paste job on my part. I have edited the answer. The notation `for(Type name : iterable)` was introduced in Java 1.5. It allows you to iterate over any `Iterable` or array. The name and type are the name and type of each element that you process within your loop. Originally `clubs` was a collection of `Club` elements. – Mad Physicist Nov 07 '14 at 01:53
  • @GiovanniMarcolla. You should select this answer by clicking on the check mark next to it if it works well for you. – Mad Physicist Nov 07 '14 at 19:50
0

Try going to every single line and reading in case of anything going wrong

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
String line = null;
while ((line = reader.readLine()) != null) {

//AFTER READING 

split the line, then loop through the parts of the line. then add the element into a list.
0

The problem is that you are trying to read objects as characters and not as "objects". There is a simple way of reading objects from file using ObjectInputStream (similar to ObjectOutputStream for writing objects). So the code for reading arraylist from file would be something like:

ArrayList<String> input = new ArrayList<String>();
try{
      FileInputStream fin = new FileInputStream("src//ServerInfo.txt");
      ObjectInputStream ois = new ObjectInputStream(fin);
      input = (ArrayList) ois.readObject();
      ois.close();
      fin.close();
   }
catch(Exception e){
     e.getMessage();
}
for(int i=0;i<input.size();i++){
    System.out.println(input.get(i);
}
pvs
  • 1
  • 1