前景:
是这样的
现在有个
public class Book{
private  String bookname;//书籍的名字
private  String duthor;//书的作者
private  String isbn;//书的ISBN
private  double price;//书的价额
private  String booktype;//书的类型
private  String published;//出版社名字
private  int bookid;
public  int count;
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getDuthor() {
return duthor;
}
public void setDuthor(String duthor) {
this.duthor = duthor;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getBooktype() {
return booktype;
}
public void setBooktype(String booktype) {
this.booktype = booktype;
}
public String getPublished() {
return published;
}
public void setPublished(String published) {
this.published = published;
}
}现在把Book类添加到List进去import java.util.List;public class addBook{
public static void addU(List list,Object o){
list.add(o);
}
}
现在就是添加到List后要保存到文件进去,这边要怎么写代码呢?然后每次还可以读取
感觉有点蒙希望大家帮忙

解决方案 »

  1.   

    Book.java
    package test;public class Book {
    private String bookname; private String duthor; private String isbn; private double price; private String booktype; private String published; private int bookid; public int count; public String toString() {
    return this.bookname + ";" + this.duthor + ";" + this.isbn + ";"
    + this.price + ";" + this.booktype + ";" + this.published + ";"
    + this.bookid + ";" + this.count;
    } public String getBookname() {
    return bookname;
    } public void setBookname(String bookname) {
    this.bookname = bookname;
    } public String getDuthor() {
    return duthor;
    } public void setDuthor(String duthor) {
    this.duthor = duthor;
    } public String getIsbn() {
    return isbn;
    } public void setIsbn(String isbn) {
    this.isbn = isbn;
    } public double getPrice() {
    return price;
    } public void setPrice(double price) {
    this.price = price;
    } public String getBooktype() {
    return booktype;
    } public void setBooktype(String booktype) {
    this.booktype = booktype;
    } public String getPublished() {
    return published;
    } public void setPublished(String published) {
    this.published = published;
    } public int getBookid() {
    return bookid;
    } public void setBookid(int bookid) {
    this.bookid = bookid;
    } public int getCount() {
    return count;
    } public void setCount(int count) {
    this.count = count;
    }}
    BookUtil.java
    package test;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.util.ArrayList;public class BookUtil {
    public static ArrayList<Book> readFile(String fileName) throws Exception {
    ArrayList<Book> lst = null;
    String s = null;
    File f = new File(fileName); try {
    BufferedReader br = new BufferedReader(new InputStreamReader(
    new FileInputStream(f)));
    // start line 5 test step list
    lst = new ArrayList<Book>();
    while ((s = br.readLine()) != null) {
    String[] line = s.split("\0");
    Book b = new Book();
    b.setBookname(line[0]);
    b.setDuthor(line[1]);
    b.setIsbn(line[2]);
    b.setPrice(Double.valueOf(line[3]));
    b.setBooktype(line[4]);
    b.setPublished(line[5]);
    b.setBookid(Integer.parseInt(line[6]));
    b.setCount(Integer.parseInt(line[7]));
    lst.add(b);
    }
    System.out.println("read file finished");
    } catch (Exception e) {
    throw e;
    }
    return lst;
    } public static void saveFile(String fileName, ArrayList<Book> lst)
    throws Exception {
    try {
    FileWriter fw = new FileWriter(fileName);
    String content = "";
    for (int i = 0; i < lst.size(); i++) {
    Book b = (Book) lst.get(i);
    String line = b.getBookname() + "\0" + b.getDuthor() + "\0"
    + b.getIsbn() + "\0" + String.valueOf(b.getPrice())
    + "\0" + b.getBooktype() + "\0" + b.getPublished()
    + "\0" + b.getBookid() + "\0" + b.getCount() + "\n";
    content += line; }
    fw.write(content);
    fw.flush();
    fw.close();
    System.out.println("save file finished");
    } catch (Exception e) {
    throw e;
    }
    } public static void main(String[] args) {
    try {
    ArrayList<Book> lst = new ArrayList<Book>();
    int i = 0;
    while (i++ < 5) {
    Book b = new Book();
    b.setBookname("line[0]" + i);
    b.setDuthor("line[1]" + i);
    b.setIsbn("line[2]" + i);
    b.setPrice(1.33);
    b.setBooktype("b[4]" + i);
    b.setPublished("b[5]" + i);
    b.setBookid(i + 1);
    b.setCount(i + 1);
    lst.add(b);
    }
    String fileName = "E:\\result.dat";
    BookUtil.saveFile(fileName, lst);
    ArrayList lst1 = BookUtil.readFile(fileName);
    for (int j = 0; j < lst1.size(); j++) {
    System.out.println(lst1.get(j).toString());
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}