1. A programmer friend of mine will use this method to save information before exiting the program.
2. The data from the list is not saved when the user closes his application. My method will save the data from the list to the selected file.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public void saveData(String path) {
try {
FileWriter fileWriter = new FileWriter(new File(path));
Node cur = head;
while (cur != null) {
fileWriter.write(cur.data + "\n");
fileWriter.flush();
cur = cur.next;
}
fileWriter.close();
} catch (IOException e) {
}
}
public static void main(String[] args) {
MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.saveData("test.txt");
}
}
Comments
So much helpful thanks a lot
Leave a comment