二十二、I/O框架

1、什么是流

I. 内存与存储设备之间传输数据的通道
2、流的分类

I. 方向(重点)

(1) 输入流
(2) 输出流
II. 单位
(1) 字节流
(2) 字符流
III. 功能
(1) 节点流
(2) 过滤流

3、字节流

I. 字节流的父类
(1) InputStream(抽象类)
(2) OutputStream(抽象类)
在这里插入图片描述

II. 字节节点流
(1) FileInputStream
(2) FileOutputStream
III. 字节过滤流
(1) BufferedOutputStream
(2) BufferedInputStream
(3) 提供了IO效率,减少访问磁盘的次数。数据存放在缓冲区中。flush刷新缓冲区,提交数据

FileOutputStream fos = new FileOutputStream("Files\\target.txt");
		//增强字节流
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		bos.write('a');
		bos.write('a');
		bos.write('a');
		bos.write('a');
		bos.write('a');	
		bos.flush(); //刷新缓冲区(将缓冲区的数据一次性写入到文本中)
		bos.write('c');
		bos.write('d');
		bos.close();
		FileInputStream fis = new FileInputStream("Files\\target.txt");
		BufferedInputStream bis = new BufferedInputStream(fis);

IV. 对象流
(1) ObjectOutputStream
(2) ObjectInputStream
(3) 增强了读写8种基本数据类型和字符串功能
(4) 读写对象,实现对象的持久化存储

	OutputStream os = new FileOutputStream("Files\\target.txt");
		ObjectOutputStream oos = new ObjectOutputStream(os); //对象流
		Student stu = new Student("maomao", "nan", 16, 33.0);
		Student stu1 = new Student("gougou", "nan", 16, 33.0);
		Student stu2 = new Student("wawa", "nan", 16, 33.0);
		oos.writeObject(stu);
		oos.writeObject(stu1);
		oos.writeObject(stu2);
		oos.flush();
		
		InputStream is = new FileInputStream("Files\\target.txt");
		ObjectInputStream ois = new ObjectInputStream(is);
		while(true) {
			try {
				Object obj =  ois.readObject();
				System.out.println(obj);
			}catch(Exception e) {
				break;
			}
			
		}

V. 序列化/反序列化
序列化:把对象转化为可传输的字节序列过程称为序列化。

反序列化:把字节序列还原为对象的过程称为反序列化。

(1) 必须实现Serializable接口。 标识序列化功能
(2) 必须保证所有属性均支持序列化。
(3) Transient修饰的为临时属性,不参与序列化
(4) 读取到文件末尾时:java.IO.EOFException

4、字符编码

I. GBK 简体中文、扩展
II. UTF-8 针对Unicode的可变长度字符编码
III. GB2312 简体中文
IV. 当编码和解码方式不一致时,会出现乱码

String str = "你好世界12323";
		byte[] bs = str.getBytes("BIG5");  //获取字节数组,用big5编码
		
		for (int i = 0; i < bs.length; i++) {
			System.out.println(bs[i]);
		}
		
		String str2 = new String(bs,"BIG5"); //用big5解码,获取字符串
		System.out.println(str2);

5、字符流

I. 字符流的父类
(1) Reader
(2) Writer
在这里插入图片描述

II. 字符节点流
(1) FileWriter
(2) FileReader
在这里插入图片描述

public static void main(String[] args) throws IOException {
		FileWriter fw = new FileWriter("Files\\chars.txt"); //字符流
		fw.write("nihaoa\n");
		char[] chars = new char[] {'你','好'};
		fw.write(chars); //写char数组
		fw.flush();
		FileReader fr = new FileReader("Files\\chars.txt"); //字符流
		char[] cache = new char[4];
		while(true) {
			int n = fr.read(cache); //读入数组
			if(n == -1) {
				break;
			}
			for(int i = 0;i < n; i++) {
				System.out.print(cache[i]);
			}
			System.out.println();
		}
	}

III. 字符过滤流
(1) BufferedWriter/PrintWriter
(2) BufferedReader
(3) 支持写一行、读一行
在这里插入图片描述
BufferedWriter PrintWriter

public static void main(String[] args) throws IOException {
		FileWriter fw = new FileWriter("Files\\char.txt");
		BufferedWriter bw = new BufferedWriter(fw);  
		bw.write("hello");
		bw.newLine();
		PrintWriter pw = new PrintWriter(fw); //更好用
		pw.println("hello world");
		pw.println("hello world");
		pw.println("hello world");
		pw.close();
		FileReader fr = new FileReader("Files\\char.txt");
		BufferedReader br = new BufferedReader(fr);
		while(true) {
			String str = br.readLine();
			if(str == null) {
				break;
			}
			System.out.println(str);
		}
	}

6、字符节点流

I. 桥转换流
(1) InputStreamReader
(2) OutputStreamWriter
(3) 可将字节流转换为字符流,可设置编码方式(编码与解码要一致)
II. 使用步骤
(1) 创建节点流
(2) [创建过滤流,设置字符编码集]
(3) 封装过滤流
(4) 读写数据
(5) 关闭流

//字节流
		OutputStream os = new FileOutputStream("Files\\convert.txt");
		//字符流
		OutputStreamWriter osw = new OutputStreamWriter(os,"GBK");
		//字符过滤流
		PrintWriter pw = new PrintWriter(osw);
		
		pw.println("今天天气真好啊");
		pw.println("我们出去玩吧");
		osw.flush();
		//字节流
		InputStream is = new FileInputStream("Files\\convert.txt"); 
		//字符流
		InputStreamReader isr = new InputStreamReader(is,"GBK");
		//字符过滤流
		BufferedReader bw = new BufferedReader(isr);
		
		while(true) {
			String str = bw.readLine();
			if(str == null) {
				break;
			}
			System.out.println(str);
		}
		
	}

7、File

在这里插入图片描述

I. FileFilter接口

	File file = new File("D:\\eclipse");
	File[] files = file.listFiles(new MyFilter());//获取所有文件,返回file类型
	System.out.println(files.length);
//筛选条件	
class MyFilter implements FileFilter{
	@Override
	public boolean accept(File pathname) {
		if(pathname.isFile()) {
			if(pathname.getName().endsWith(".exe")) {
				return true;
			}
		}
		return false;
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值