I/O流分类
- 按照方向划分:输入流,输出流
- 按照处理单元划分:字节流,字符流
- 按照功能划分:节点流,处理流
读入文件
public static void main(String[] args) throws IOException {
//对文件进行操作时,必须将文件封装为File类
File f = new File("E:\\System\\5_12\\test1.txt");
FileReader fr = new FileReader(f);
int n1 = fr.read();
//读数据时,没有数据读入为-1
while(n1!=-1){
System.out.println(n1);
n1 = fr.read();
}
fr.close();
}
输入数据
public static void main(String[] args) throws IOException {
//读入文件
String str = "abc你好";
File f = new File("E:\\System\\5_12\\test2.txt");
FileWriter fw = new FileWriter(f);
fw.write(str);
fw.close();
}
图书管理系统(使用IO流
package Test_5_12;
import Day05.list;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class BookTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
loop:while (true){
System.out.println("-----------欢迎来到图书管理系统-----------");
Scanner sc = new Scanner(System.in);
System.out.println("1:上新书籍");
System.out.println("2:下架书籍");
System.out.println("3:展示书籍信息");
System.out.println("4:退出");
System.out.println("请输入您的选择");
String choose = sc.next();
switch (choose){
case "1":
addBook();
break;
case "2":
deleteBook();
break;
case "3":
searchBook();
break;
case "4":
System.out.println("退出成功,欢迎下次访问!");;
break loop;
default:
System.out.println("输入的数字错误!");
break;
}
}
}
//上新书籍
public static void addBook() throws IOException, ClassNotFoundException {
File f = new File("E:\\System\\5_12\\booktest1.txt");
System.out.println("上新书籍功能启动");
System.out.println("请输入要上新的书籍编号");
if(f.exists()==true){
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList list = (ArrayList)(ois.readObject());
int bookId;
Scanner sc = new Scanner(System.in);
while(true){
bookId = sc.nextInt();
int repeat = flag(list,bookId);
if (repeat>=0){
System.out.println("您要上新的书籍编号不唯一,请重新输入");
}else {
break;
}
}
System.out.println("请输入要上新书籍的名称");
Scanner sc1 = new Scanner(System.in);
String bookName = sc1.next();
System.out.println("请输入要该书籍的作者");
Scanner sc2 = new Scanner(System.in);
String bookAuthor = sc2.next();
Book b1 = new Book();
b1.setBookId(bookId);
b1.setBookName(bookName);
b1.setBookAuthor(bookAuthor);
list.add(b1);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
}
else {
ArrayList list = new ArrayList();
int bookId;
Scanner sc = new Scanner(System.in);
while(true){
bookId = sc.nextInt();
int repeat = flag(list,bookId);
if (repeat>=0){
System.out.println("您要上新的书籍编号不唯一,请重新输入");
}else {
break;
}
}
System.out.println("请输入要上新书籍的名称");
Scanner sc1 = new Scanner(System.in);
String bookName = sc1.next();
System.out.println("请输入要该书籍的作者");
Scanner sc2 = new Scanner(System.in);
String bookAuthor = sc2.next();
Book b1 = new Book();
b1.setBookId(bookId);
b1.setBookName(bookName);
b1.setBookAuthor(bookAuthor);
list.add(b1);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();//oos为外面流,fos为里面流
}
}
//编号唯一性判断
public static int flag(ArrayList<Book> list,int id){
int res = -1;
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getBookId()==id){
res = i;
}
}
return res;
}
public static void deleteBook() throws IOException, ClassNotFoundException {
System.out.println("下架书籍功能启动!");
//从文件中读取数据
File f = new File("E:\\System\\5_12\\booktest1.txt");
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList list = (ArrayList)(ois.readObject());
System.out.println("请输入要下架的书籍编号");
Scanner sc = new Scanner(System.in);
int bookId = sc.nextInt();
int res = flag(list,bookId);
if (res<0){
System.out.println("不存在要下架的书籍");
return;
}
list.remove(res);
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();//oos为外面流,fos为里面流
System.out.println("下架成功!");
System.out.println("下架书籍功能结束!");
}
public static void searchBook() throws IOException, ClassNotFoundException {
System.out.println("展示书籍功能启动!");
//从文件中读取数据
File f = new File("E:\\System\\5_12\\booktest1.txt");
if(f.exists()==true){
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList list = (ArrayList)(ois.readObject());
if(list.size()==0){
System.out.println("当前系统中没有书,请上新后再展示");
return;
}
for (int i = 0; i < list.size(); i++) {
Book b = (Book)(list.get(i));
System.out.println(b.getBookId()+"-----"+b.getBookName()+"-----"+b.getBookAuthor());
}
}else {
System.out.println("当前没有书籍可以展示,请上新后再展示");
}
System.out.println("查询书籍功能结束!");
}
}
多线程运行:多线程之间存在资源争抢
单向,双向通信
XML(EXtensible Markup Language):可扩展标记语言