Results 1 to 7 of 7
Thread: Saving ArrayList to File
- 12-02-2008, 02:00 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 45
- Rep Power
- 0
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.
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?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) { } } } }
- 12-02-2008, 07:39 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What unchecked cast warning you get? Seems to me there is no any warnings.
- 12-02-2008, 05:40 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 45
- Rep Power
- 0
Here is the warning I get from the compiler:
It points to the bold line in my code example.warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.ArrayList<T>
- 12-03-2008, 07:24 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm still not find any error there, I've test that code on my IDE and compiled fine. No warning either.
- 12-06-2008, 09:52 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
remove all instances of <T> from your code
-
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:
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.Java Code:@SuppressWarnings("unchecked") ArrayList myList = new ArrayList();
- 12-08-2008, 10:19 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ya, I think you should read little more about annotations in Java. So you can have clear idea about warnings.
Similar Threads
-
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 10Last Post: 10-24-2008, 07:21 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-24-2008, 12:31 AM -
Saving to a Text File
By jadaleus in forum Advanced JavaReplies: 2Last Post: 10-17-2008, 06:50 PM -
Saving data in an XML file
By Thez in forum New To JavaReplies: 1Last Post: 12-08-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks