本文共 4535 字,大约阅读时间需要 15 分钟。
public class Book { private String BookId; private String BookName; private String BookAuthor; public Book() { // TODO 自动生成的构造函数存根 } public String getBookId() { return BookId; } public void setBookId(String bookId) { BookId = bookId; } public String getBookName() { return BookName; } public void setBookName(String bookName) { BookName = bookName; } public String getBookAuthor() { return BookAuthor; } public void setBookAuthor(String bookAuthor) { BookAuthor = bookAuthor; } public double getBookPrice() { return BookPrice; } public void setBookPrice(double bookPrice) { BookPrice = bookPrice; } @Override public String toString() { return "Book [BookId=" + BookId + ", BookName=" + BookName + ", BookAuthor=" + BookAuthor + ", BookPrice=" + BookPrice + "]"; } private double BookPrice; public Book(String bookId, String bookName, String bookAuthor, double bookPrice) { super(); BookId = bookId; BookName = bookName; BookAuthor = bookAuthor; BookPrice = bookPrice; }}
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Scanner;import com.candy.Book.Book;public class BookDao { public static void newbuild() throws IOException { File file = new File("D:\\Book.txt"); Book book = new Book(); // 键入id信息 System.out.println("请输入书籍ID"); Scanner scId = new Scanner(System.in); book.setBookId(scId.nextLine()); // 键入姓名信息 System.out.println("请输入书籍名称"); Scanner scName = new Scanner(System.in); book.setBookName(scName.nextLine()); // 键入作者信息 System.out.println("请输入书籍作者"); Scanner scAuthor = new Scanner(System.in); book.setBookAuthor(scAuthor.nextLine()); // 键入价格信息 System.out.println("请输入书籍价格"); Scanner scPrice = new Scanner(System.in); book.setBookPrice(Double.parseDouble(scPrice.nextLine())); // 读取文件 BufferedReader reader = null; StringBuffer sbf = new StringBuffer(); reader = new BufferedReader(new FileReader(file)); String tempStr; while ((tempStr = reader.readLine()) != null) { sbf.append(tempStr); } reader.close(); String existContent = sbf.toString(); System.out.println("获取到文件内容:" + existContent); // 写入文件 BufferedWriter out = new BufferedWriter(new FileWriter("D:\\BOOK.TXT")); String bookStr = book.getBookId() + "," + book.getBookName() + "," + book.getBookAuthor() + "," + book.getBookPrice(); System.out.println("输入的内容:" + bookStr); // 判断字符串内容是否为空 if ("".equals(existContent)) { out.write(bookStr + "#"); } else { out.write(existContent + bookStr + "#"); } out.close(); } public static void query() throws IOException { File file = new File("D:\\Book.txt"); BufferedReader reader2 = null; StringBuffer sbf2 = new StringBuffer(); reader2 = new BufferedReader(new FileReader(file)); String tempStr2; while ((tempStr2 = reader2.readLine()) != null) { sbf2.append(tempStr2); } reader2.close(); String existContent2 = sbf2.toString(); System.out.println("获取到文件内容:" + existContent2); String[] booksArray = existContent2.split("#"); System.out.println("总共有" + booksArray.length); if (booksArray.length > 0) { // 开始循环每本书 for (String s : booksArray) { System.out.println("===================================="); String[] bookArray = s.split(","); System.out.println("ID:" + bookArray[0]); System.out.println("NAME:" + bookArray[1]); System.out.println("AUTHOR:" + bookArray[2]); System.out.println("PRICE:" + bookArray[3]); System.out.println("===================================="); } } else { System.out.println("没有书"); } }}
import java.io.File;import java.io.IOException;import java.util.Scanner;import com.candy.Dao.BookDao;public class BookFile { public static void main(String[] args) throws IOException { File file = new File("D:\\Book.txt"); file.createNewFile(); //控制台输入 while(true) { System.out.println("请输入您想要的操作"); System.out.println("1.新建 2.查询 3.退出"); Scanner sc = new Scanner(System.in); switch (sc.nextLine()) { case "1": // 新建 BookDao.newbuild(); break; case "2": // 查询 BookDao.query(); break; case "3": // 退出 System.out.println("已退出系统"); System.exit(-1); break; default: System.out.println("输错了,重来!"); } } }}
请输入您想要的操作1.新建 2.查询 3.退出1请输入书籍ID1请输入书籍名称《蔡康永的说话之道》请输入书籍作者蔡康永请输入书籍价格24.9获取到文件内容:输入的内容:1,《蔡康永的说话之道》,蔡康永,24.9请输入您想要的操作1.新建 2.查询 3.退出2获取到文件内容:1,《蔡康永的说话之道》,蔡康永,24.9#总共有1====================================ID:1NAME:《蔡康永的说话之道》AUTHOR:蔡康永PRICE:24.9====================================请输入您想要的操作1.新建 2.查询 3.退出3已退出系统
转载地址:http://ytnwi.baihongyu.com/