To say that data is persistent, beginners may not understand it yet, but to save the data to a file, everyone knows. Why persistence is the saving of data to a file. Persistence means that when the program exits, the data in it can be retained for use when the program is run again. It is best to keep the data where it is. Of course it's in the file.
Saving to a file is the most common way to persist data, and in addition, you can save data to a database or send it to another machine, which is persistent. However, saving it in a file is the easiest way to do it. Specifically: Select the data that needs to be saved, organized in a certain format, and then write to the file. The following is a simple example:
Import java.io.*; Import java.util.ArrayList; Import Java.util.Date; Import java.util.List; /** * Persistence example. How to save the data in memory, not a certain format, anyone * can be based on their own preferences to make. Persistence requires file manipulation, so make sure you understand how to read and write files first. * * public class Persistant {//filename can be arbitrarily specified, you can open this file with a text editor (note that Notepad cannot handle the newline) static String filename = "persons.data"; public sta tic void Main (string[] args) throws Exception {//Run this program two times. The first time it creates some person objects and saves them to a file;//The second time it reads the object data from the file and displays it. if (!new File (filename). Exists ()) {Createandsave ();} else {readandshow ();}} Generate and save the person object private static void Createandsave () throws IOException {list<person> persons = Createpersons (); Savepersons (persons); //Read and display the person object private static void Readandshow () throws IOException {list<person> persons = readpersons (); SH Owpersons (persons); //Create the person object you want to save private static list<person> Createpersons () {list<person> result = new Arraylist<per Son> (); Result.add (New person ("John", New Date (), true); Result.add (New person ("Dick", New Date (), false); Result.add (New person ("Harry", New Date (), true); return result; //Save the Person object to the file. Save format is://1, each person line//2, each row in the name, birthday, male three attribute values, tab-separated//3, Birthday property to save the number of milliseconds, male property is to save the string private stat IC void Savepersons (list<person> persons) throws IOException {//Generate file contents String data = ""; for [person Person:persons] {data + + getpersonstring (person) + '/n ';}//Save file content FileWriter writer = new FileWriter (fil ENAME); Writer.write (data); Writer.close (); System.out.println ("The object is saved.) "); } private static String getpersonstring (person person) {return person.getname () + '/t ' + person.getbirthday (). GetTime () + " /T "+ person.ismale (); ///Read the person object from the file private static list<person> Readpersons () throws IOException {list<person> result = new Arraylist<person> (); BufferedReader reader = new BufferedReader (new FileReader (filename)); String Line; while (line = Reader.readline ())!= null) {Result.add (getpersonfromstring); }//ThroughOne-line file content generates a person object private static person getpersonfromstring (String line) {string[] parts = line.split ("T");//Get delimited Three parts return to new person (parts[0],//Name new Date (Long.parselong (parts[1)),//Birth date Boolean.parseboolean (parts[2))//IS male Sex); //Show Person object private static void Showpersons (list<person> persons) {for (person person:persons) {System.out . println (Person.getname () + "," + person.getbirthday () + "," + (Person.ismale ()?) Male ":" female ")); Class Person {Private String name to be persisted///name private Date birthday;//Birthday Private Boolean male;//true for males, False indicates female person (String name, Date birthday, Boolean male) {this.name = name; this.birthday = birthday; This.male = male ; Public String GetName () {return name.} public void SetName (String name) {this.name = name;} public Date Getbirthday ( {Birthday} public void Setbirthday (Date birthday) {this.birthday = birthday;} public boolean Ismale () {Retu RN male; } public void Setmale (Boolean malE) {this.male = male;}}
PS: How to keep blank lines when pasting code
Empty lines are an important part of the code, but in the CSDN Blog to paste the code, it will be empty lines removed. The workaround is to precede the blank line with a space.