1

I have certain arraylists in my programm which I would like to write into a file so I can read them when starting the programm for a second time. Currently it works for an arraylist of persons.

The reading part:

ObjectInputStream ois = null;
                    try {
                        ois = new ObjectInputStream(new FileInputStream("test.txt"));

                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    team.setPersonList((ArrayList<Person>) ois.readObject());

The writeToFile class:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class WriteToFile {


public void write(ArrayList<Person> Data ) throws IOException, ClassNotFoundException{


   // create a new file with an ObjectOutputStream
   FileOutputStream out = new FileOutputStream("test.txt");
   ObjectOutputStream oout = new ObjectOutputStream(out);

   // write something in the file
   oout.writeObject(Data);

   // close the stream
   oout.close();

   // create an ObjectInputStream for the file we created before
   ObjectInputStream ois = null;
try {
    ois = new ObjectInputStream(new FileInputStream("test.txt"));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

   // read and print what we wrote before
   System.out.println("" + ois.readObject());


  ois.close();
}

}

In main:

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class Main {

    public static void main(String[] args) throws ClassNotFoundException, IOException{
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        Team team = new Team(scan);

        new InlogSystem(team, scan);
        ArrayList<Person> playersData = team.getPersonList();

        WriteToFile x = new WriteToFile();
        x.write(playersData);
        scan.close();

    }

}

So this is the working part, Now i want an arraylist of Strings to write into another txt file (not test like the personlist) using the same writeToFile class. Obviously the writemethod only works for an arraylist of type person, and it always saves the array into "test.txt".

How do I write this arraylist without making a new method and having alot of ambigious code? Thanks alot!

3 Answers 3

1

You can change the parameter type ArrayList<String> to ArrayList<?> or even List<?> or, what I would suggest, to Object. Then, your method is capable of writing arbitrary objects.

Using generics is useless here: It provides compile-time type checking, which wouldn't be used in your method.

By the way, your exception handling is very bad. I suggest you catch and re-throw the ClassNotFoundException as an IOException; FileNotFoundException is just a subclass of IOException and needn't be catched seperately - and why do you catch an IOException at all when your method is declared as throws IOException ?

Sign up to request clarification or add additional context in comments.

Comments

0

Amend your method to accept List instead, where T is parametrized object. Refer to generics documentation

Comments

0

Use a generic method and pass the filename as a parameter i.e.

public <T implements Serializable> void write(ArrayList<T> Data, String filename) throws IOException {

   // create a new file with an ObjectOutputStream
   FileOutputStream out = new FileOutputStream(filename);
   ObjectOutputStream oout = new ObjectOutputStream(out);

   // write something in the file
   oout.writeObject(Data);

   // close the stream
   oout.close();

   // create an ObjectInputStream for the file we created before
   ObjectInputStream ois = null;
   ois = new ObjectInputStream(new FileInputStream("test.txt"));

   // read and print what we wrote before
   System.out.println("" + ois.readObject());

   ois.close();
}

1 Comment

Better if it is <T implements Serializable>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.