博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java]用IO流知识创建控制台查询系统
阅读量:3942 次
发布时间:2019-05-24

本文共 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/

你可能感兴趣的文章
thrift的lua实现
查看>>
编写高性能的Lua代码
查看>>
Python正则表达式指南
查看>>
LUA--thrift--lib库的创建生成
查看>>
Shell开启扩展模式匹配shopt -s extglob
查看>>
浅谈 URI 及其转义
查看>>
nginx 优化
查看>>
openresty+lua在反向代理服务中的玩法
查看>>
ClickHouse集群搭建从0到1
查看>>
nginx实现请求的负载均衡 + keepalived实现nginx的高可用
查看>>
linux shell 中数组的定义和for循环遍历的方法
查看>>
求1!+2!+3!....+20!(java代码)
查看>>
VMware安装Ubuntu系统无法选择语言
查看>>
QT5.12安装
查看>>
Git/Github初步使用记录
查看>>
QT 开发问题合集
查看>>
Github使用问题合集
查看>>
QT多线程服务器
查看>>
Ubuntu 18.04.2 ulimit配置
查看>>
Ubuntu Mysql 安装与配置
查看>>