Giovanni Marcolla
Giovanni Marcolla

Reputation: 113

reading file created from arraylist

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 [email protected] 
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
[email protected]
Administrator

Upvotes: 1

Views: 1239

Answers (3)

Mad Physicist
Mad Physicist

Reputation: 114310

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.

Upvotes: 1

pvs
pvs

Reputation: 1

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

Upvotes: 0

vemacsrobot
vemacsrobot

Reputation: 9

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.

Upvotes: 0

Related Questions