so I am making a program that has a linkedlist, I need it to store the info from the list when the program exits, and load when it starts
is there anyway to do that ? maybe other than the file
thanks in advance!
so I am making a program that has a linkedlist, I need it to store the info from the list when the program exits, and load when it starts
is there anyway to do that ? maybe other than the file
thanks in advance!
I would suggest writing the contents to a text file, however as Elliott Frisch mentions there are several other methods. The simplest to me seems to be writing to a text file, using Serialization.
Note: This requires that the list be defined as type LinkedList not as List, because whereas LinkedList is serializable, List is not.
What I mean:
OK : LinkedList<String> list = new LinkedList<String>();
Not OK : List<String> list = new LinkedList<String>();
Writing to the file:
FileOutputStream fout = new FileOutputStream(“tmp.txt”);
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(myList);
Reading from the file:
FileInputStream fin = new FileInputStream(“tmp.txt”);
ObjectInputStream in = new ObjectInputStream(fin);
LinkedList<WhateverData> myList = (LinkedList<WhateverData>) in.readObject();
I believe that WhateverData
should also be Serializable.
Do not treat this as an answer, rather treat it as a comment.
Have you tried thinking in context of serialization. For eg: If you a list of numbers that your are using for linked list, you can serialize the integer array.
Later when the program starts, you can de-serialize and use to reformed array of numbers to populate your linked list.
Hope this helps.
There are many ways of doing this, but for beginners saving into a text file is probably the best way to go. Loop through your linked list and store everything in a designated .txt file using PrintWriter
(or any other class in Java):
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
CustomList list = new CustomList();//not sure what your list is called
//iterate over list {
writer.println(list.get(currentPosition));
}
writer.close();//saves file
Source: How do I create a file and write to it in Java?
You can do the same thing when reading a file using BufferedReader
:
BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
CustomList list = new CustomList();//not sure what your list is called
String line = null;
while ((line = reader.readLine()) != null) {
list.add(new CustomeNode(line));
}
Source: Java: How to read a text file
Note: This is pseudo code.
In general there are three main steps to achieve your goal -
Your persistent store may be almost any form of storage. It might be a remote database server (or file system), it might be an in memory system, it might be backed by a "well-known" local filePath. Here's a little more reading on persistence strategies.
You must use a file, there is no other way. Make a format for your data, write to file, read it in again. That's what files are, they are permanent data storage mechanisms separate to programs.