Java Stored book information

Using the file input / output streams completion information stored books and books read out again.

Every school school season will be a number of books for new textbooks, these books need to save information to a file, then print them out easily see the teacher. The following programming, using the file input / output stream storage function is completed and reading of book information, the specific steps are as follows.

1. Create a Book class, contains no, name and price 3 attributes in the class, respectively number of books, books and books the name of the unit price. The method also includes two write () and read (), respectively, for writing information to a disk file books and book information is read from the disk file and printed to the console.

In addition, toString () method and a class constructor takes three parameters included in the Product class, specifically as follows:

public class Book {
    private int no; // 编号
    private String name; // 名称
    private double price; // 单价

    public Book(int no, String name, double price) {
        this.no = no;
        this.name = name;
        this.price = price;
    }

    public String toString() {
        return "图书编号:" + this.no + ",图书名称:" + this.name + ",图书单价:" + this.price + "\n";
    }

    public static void write(List books) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("E:\\myJava\\books.txt"); // 创建FileWriter对象
            for (int i = 0; i < books.size(); i++) {
                fw.write(books.get(i).toString()); // 循环写入
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void read() {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("E:\\myJava\\books.txt");
            br = new BufferedReader(fr); // 创建BufferedReader对象
            String str = "";
            while ((str = br.readLine()) != null) { // 循环读取每行数据
                System.out.println(str); // 输出读取的内容
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Respectively, using the character file and output streams FileWriter input stream BufferedReader character buffer store and read the complete function of book information.

2 write test classes Test, create two Book objects, and save both objects to a List collection, then transfer to a collection of objects List Book class write () method, the F: \ product.txt file written book information. Product class last call read () method reads the contents of the file, as follows:

public class Test {
    public static void main(String[] args) {
        Book book1 = new Book(1001, "百度搜索引擎", 159);
        Book book2 = new Book(1002, "百度搜索引擎", 259);
        List books = new ArrayList();
        books.add(book1);
        books.add(book2);
        Book.write(books);
        System.out.println("********************图书信息******************");
        Book.read();
    }
}

3. Book information to run the program, the output, as shown below. Open E: \ myJava \ books.txt file, the contents of the file shown in Fig.

********************图书信息******************
图书编号:1001,图书名称:百度搜索引擎,图书单价:159.0
图书编号:1002,图书名称:百度搜索引擎,图书单价:259.0
Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104711269