Results 1 to 7 of 7
  1. #1
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default Saving ArrayList to File

    Is it possible to write an ArrayList holding an unknown type to a file and read it back later? Say I have an ArrayList of apples and and ArrayList of thumbtacks. I know it's either one or the other. Apples and thumbtacks don't have a common ancestor besides Object. Rather than write a method to handle each type, I'd like to just have one method that will handle any type. I've read about generics, but nothing I do is working. Apple and ThumbTack are both Serializable.

    Java Code:
    import java.util.*;
    import java.io.*;
    ...
       ArrayList<Apple> apples = new ArrayList<Apple>();
       //[I]populate apples here[/I]
       handleList(apples, "somefile", true); //[I]no warning[/I]
       ArrayList<ThumbTack> thumbtacks = new ArrayList<ThumbTack>();
       //[I]populate thumbtacks here[/I]
       handleList(thumbtacks, "someotherfile", true); //[I]no warning[/I]
    ...
       ArrayList<Apple> otherApples = new ArrayList<Apple>();
       handleList(otherApples, "somefile", false); //[I]unchecked cast warning[/I]
    ...
    private <T> void handleList(ArrayList<T> list, String filename, boolean save) {
       File f = new File(filename);
       if (f.exists()) {
          if (save) {
             try {
                FileOutputStream fos = new FileOutputStream(f);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(list);
                fos.close();
             } catch (IOException ioe) { }
          } else {
             try {
                FileInputStream fis = new FileInputStream(f);
                ObjectInputStream ois = new ObjectInputStream(fis);
                try {
                   [B]list = (ArrayList<T>) ois.readObject();[/B] //[I]bad monkey[/I]
                } catch (ClassNotFoundException cnfe) { }
                fis.close();
             } catch (IOException ioe) { }
          }
       }
    }
    Code is just an example to hopefully show you what I'm trying to do. I understand that there's no way to know when I read a file that it will be of the type I'm looking for and could be an unsafe cast. But how do I fix it? Or is it just a warning and I can believe that my application will only be dealing with known types and ignore the warning?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default

    Here is the warning I get from the compiler:

    warning: [unchecked] unchecked cast
    found : java.lang.Object
    required: java.util.ArrayList<T>
    It points to the bold line in my code example.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm still not find any error there, I've test that code on my IDE and compiled fine. No warning either.

  5. #5
    w3user is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    remove all instances of <T> from your code

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's a warning, not an error. You can either just ignore it or use the proper annotation so that the compiler won't complain. This works for me:
    Java Code:
    @SuppressWarnings("unchecked")
    ArrayList myList = new ArrayList();
    But having said that, I don't like your idea. An ArrayList should know what it is holding be that a class type or an interface type else you're program can have nasty surprises down the line.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 08:21 PM
  2. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 01:31 AM
  3. Saving to a Text File
    By jadaleus in forum Advanced Java
    Replies: 2
    Last Post: 10-17-2008, 07:50 PM
  4. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •