說數(shù)據(jù)持久化,初學者可能還不太懂;但要說把數(shù)據(jù)保存到文件,這誰都懂了。為什么說持久化就是把數(shù)據(jù)保存到文件?持久化指的就是當程序退出后,其中的數(shù)據(jù)能夠保留下來,供程序再次運行的時候使用。這些數(shù)據(jù)保留在什么地方最好呢?當然是文件里。
保存到文件是數(shù)據(jù)持久化最常用的方式,除此之外,還可以把數(shù)據(jù)保存到數(shù)據(jù)庫,或者發(fā)送到其它機器,這都是持久化。不過保存在文件是最簡單的方式。具體來說就是:選取需要保存的數(shù)據(jù),按照一定的格式組織起來,然后寫入文件。下面是一個簡單的例子:
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
-
-
-
-
-
- public class Persistant {
-
-
- static String filename = "persons.data";
-
- public static void main(String[] args) throws Exception {
-
-
-
- if (!new File(filename).exists()) {
- createAndSave();
- } else {
- readAndShow();
- }
- }
-
-
- private static void createAndSave() throws IOException {
- List<Person> persons = createPersons();
- savePersons(persons);
- }
-
-
- private static void readAndShow() throws IOException {
- List<Person> persons = readPersons();
- showPersons(persons);
- }
-
-
- private static List<Person> createPersons() {
- List<Person> result = new ArrayList<Person>();
- result.add(new Person("張三", new Date(), true));
- result.add(new Person("李四", new Date(), false));
- result.add(new Person("王五", new Date(), true));
- return result;
- }
-
-
-
-
-
- private static void savePersons(List<Person> persons) throws IOException {
-
-
- String data = "";
- for (Person person : persons) {
- data += getPersonString(person) + "\n";
- }
-
-
- FileWriter writer = new FileWriter(filename);
- writer.write(data);
- writer.close();
- System.out.println("對象保存完畢。");
- }
-
- private static String getPersonString(Person person) {
- return person.getName() + "\t" + person.getBirthday().getTime() + "\t" + person.isMale();
- }
-
-
- 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(line));
- }
-
- return result;
- }
-
-
- private static Person getPersonFromString(String line) {
- String[] parts = line.split("\t");
-
- return new Person(
- parts[0],
- new Date(Long.parseLong(parts[1])),
- Boolean.parseBoolean(parts[2])
- );
- }
-
-
- private static void showPersons(List<Person> persons) {
- for (Person person : persons) {
- System.out.println(person.getName() + ", " +
- person.getBirthday() + ", " +
- (person.isMale() ? "男" : "女"));
- }
- }
- }
-
-
- class Person {
-
- private String name;
-
- private Date birthday;
-
- private boolean male;
-
- 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() {
- return birthday;
- }
-
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
-
- public boolean isMale() {
- return male;
- }
-
- public void setMale(boolean male) {
- this.male = male;
- }
- }
轉:http://blog.csdn.net/YidingHe/archive/2009/03/09/3971073.aspx
posted on 2009-03-10 07:52
Werther 閱讀(4113)
評論(0) 編輯 收藏 所屬分類:
10.Java