说数据持久化,初学者可能还不太懂;但要说把数据保存到文件,这谁都懂了。为什么说持久化就是把数据保存到文件?持久化指的就是当程序退出后,其中的数据能够保留下来,供程序再次运行的时候使用。这些数据保留在什么地方最好呢?当然是文件里。
保存到文件是数据持久化最常用的方式,除此之外,还可以把数据保存到数据库,或者发送到其它机器,这都是持久化。不过保存在文件是最简单的方式。具体来说就是:选取需要保存的数据,按照一定的格式组织起来,然后写入文件。下面是一个简单的例子:
- 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 阅读(4105)
评论(0) 编辑 收藏 所属分类:
10.Java