1

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!

user2602020
  • 13
  • 1
  • 1
  • 3
  • If you don't want to store it into a file, I doubt there's another way to store it once the JVM has quit... I can post a solution for reading/writing from a file if you would like. – Vineet Kosaraju Dec 01 '13 at 01:36
  • 1
    Yes. You can store it in another file (maybe stored in the user's home folder). A database (local or remote). A remote (file) server. Use a `Storage Area Network`. Can you be more specific? – Elliott Frisch Dec 01 '13 at 01:37

5 Answers5

2

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.

Vineet Kosaraju
  • 5,572
  • 2
  • 19
  • 21
1

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.

Sameer Sawla
  • 729
  • 6
  • 20
1

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.

Community
  • 1
  • 1
1

In general there are three main steps to achieve your goal -

  1. Check if the persistent store has a linked list (if so retrieve it, otherwise create a linked list)
  2. Perform processing on Linked List (Add, Remove, Edit, Etc)
  3. Store Linked list to persistent store

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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.

HonestHeuristic
  • 336
  • 2
  • 14