16.IO流

IO流

I:input 读入 (字符流 字节流)------》持久化数据—》临时数据

o:output 写出 (字符流 字节流)-------》临时数据----》持久化数据

概念:将数据通过方法变为一连串字节或者字符 而IO流就是传输字节或字符的载体

File文件类

​ java提供的可操作本机文件目录的方法

​ 构造方法

	File file = new File(String path);
	File file = new File(String parent,String child);
	File file = new File(File,String);
		File file1 = new File("E:\\files\\一阶段\\class63\\7.14作业\\7.14作业.txt");
		File file = new File("E:\\files\\一阶段\\class63\\7.14作业\\abc.txt");
		
		//文件名
		System.out.println(file.getName());
		//完整路径
		System.out.println(file.getPath());
		System.out.println(file.toString());
		
		//文件内容字节数
		System.out.println(file.length());
		System.out.println(file1.length());
		
		//判断该路径是否存在
		if(file.exists()) {
			System.out.println(file.length());
			//删除当前文件
			file.delete();
		}else {
			//创建文件
			file.createNewFile();
		}
		
		File file2 = new File("E:\\files\\一阶段\\class63\\7.14作业\\Chesses");
		if(!file2.exists()) {
			//创建文件夹
			file2.mkdir();
			//file2.mkdirs();//创建多级文件夹
		}else {
			file2.delete();
		}
		
		//判断是否是文件夹
		System.out.println(file.isDirectory());
		//判断是否是文件
		System.out.println(file.isFile());

String toString() 输出文件的路径

String getPath() 输出文件的路径

String getName() 输出文件对象的名称

boolean isDirectory() 判断是否是文件夹

boolean isFile() 判断是否是文件

boolean exists() 判断是否存在

boolean createNewFile() 创建文件

boolean mkdir() 创建文件夹

boolean mkdirs() 创建多级文件夹

boolean delete() 删除文件、文件夹

String[] list() 返回路径下所有文件及文件夹名称的数组

File[] listFiles() 返回路径下所有文件及文件夹对象的数组

		File file = new File("E:\\files\\一阶段\\class63\\7.14作业");
		String[] names = file.list();
		for(String s : names) {
			System.out.println(s);
		}
		
		File[] lf = file.listFiles();
		for(File f : lf) {
			System.out.println(f);
		}

练习:

选择目录下循环新增文件夹和文件 名称由用户输入 判断如果重名则提示信息并重新输入

包含3个java文件

查询这个目录下有多少文件 其中有多少文件夹 多少文件

byte[] getBytes(); 输出字节数组

String new String(byte,int start,int len)

IO流分类

​ 读入流 写出流

字节流 InputStream OutputStream

字符流 Reader Writer

字节流

​ 字节读入流 FileInputStream

​ int read() 读取一个字节 返回字节所对应的ASCII值 int类型 没有读到任何字节 返回-1

​ int read(byte[]) 一次性读取最多缓冲区大小的字节 返回真正读取的字节数 没有读到任何字节 -1

案例:从某个盘符中的文件内 读入文字信息 到程序中

//1.获取要读入的资源目录
File file = new File("E:\\files\\一阶段\\class63\\7.14作业\\Chesses\\abc.txt");
//2.创建字节读入流对象
FileInputStream fis = new FileInputStream(file);

//		一个字节一个字节的读入
//		for(int i =0;i<file.length();i++){
//			int read = fis.read();//读取一个字节 返回字节所对应的ASCII码
//			System.out.print((char)read);
//		}
//3.创建缓冲区 字节数组
		byte[] b = new byte[2];
		int l = 0;//记录每次读取的真实字节数
//4.调用带参读入方法
		while((l = fis.read(b))!=-1) {//每次读入最多缓冲区大小的字节数
//5.拼接字节
			System.out.print(new String(b,0,l));//将每次读入的字节进行拼接展示
		}

​ 字节写出流 FileOutputStream

​ void write(int) 写出ASCII码为参数的字节

​ void write(byte[]) 将一个字节数组写出

​ void write(byte[],int start,int len) 将字节数组的某个部分写出

​ void flush() 刷新缓冲区 对象可以继续使用

​ void close() 关闭字节流 带有flush()功能 同时对象不可继续使用

		//确定写出目标
		File file = new File("E:\\files\\一阶段\\class63\\7.14作业\\Chesses\\abc.txt");
		//创建字节写出流对象 第二个参数 没有 默认false 覆盖原内容
		//第二个参数 true 在原内容后追加
		FileOutputStream fos = new FileOutputStream(file,true);

		fos.write(97);//写出a
		fos.write("97".getBytes());//写出97		

		//如果要写出一个换行 写出13 10
		fos.write(13);//一行末尾
		fos.write(10);//一行开头
		
		//fos.write(97);
		//fos.write(new byte[]{97,98,99});
		
		//fos.write(new byte[] {97,98,99,100,101},1,3);
		
		//将要写出的字符串 反向获取其字节数组
		String str = "中国你好中国万岁";
		byte[] bytes = str.getBytes();
		//写出这个字节数组 目标接收后 按照目标文件字符编码格式拼接
		fos.write(bytes);

		fos.flush();//刷新缓冲区字节
		//关闭资源
		fos.close();

练习:

​ 将C盘 aaa文件夹中 abc.txt 复制到C盘bbb文件夹中

字符流

​ 字符读入流 FileReader

​ int read() 每次读入一个字符 返回其对应编码集值

​ int read(char[]) 每次最多读参数数组长度的字符 存入缓冲区数组 返回实际读入的字符数

		//确定从哪个文件读入
		File file = new File("E:\\files\\abc\\aaa.txt");
		//创建字符读入流对象
		FileReader fr = new FileReader(file);
		//创建字符数组作为缓冲区  
		char[] c = new char[3];
		
		int l = 0;//记录每次实际读入的字符数
		while((l=fr.read(c))!=-1) {
            //拼接数组中的字符
			System.out.print(new String(c,0,l));
		}

​ 字符写出流 FileWriter

​ void write(String)

​ void write(String,int start,int len)

​ void write(char[])

​ void write(char[],int start,int len)

​ void write(int)

​ void flush()

​ void close()

		//设定写出目标目录
		File file = new File("E:\\files\\abc\\aaa.txt");
		//创建字符写出流对象
		FileWriter fr = new FileWriter(file,true);
		//写出一个换行符
		fr.write("\r\n");
		
		fr.write("蜗牛学院".toCharArray());

//		System.out.println((int)'中');//'中'的GBK编码值:20013		
//		fr.write(20013);//写出一个 '中' 字符
//		fr.write(20013);
//		fr.write(20013);
//		
		//字符流写出完毕后 必须刷新或者关闭写出流对象 否则不能实现写出操作
		fr.flush();
		fr.close();

练习:

​ 分别以字节流 写出 以字符流形式 读入以下内容

1.中国你好中国万岁

2.中华人民共和国万岁

3.全国人民大团结万岁

4.世界人民大团结万岁

读入后存入集合 展示集合

缓冲流

案例:分别使用字节流及字节缓冲流 复制一个视频 统计对比使用的时间

		long time1 = new Date().getTime();
		
		//视频原始地址
		File file = new File("E:\\files\\abc\\aa\\bbb.mp4");
		//字节读入流
		FileInputStream fis = new FileInputStream(file);
		//字节读入缓冲流
		BufferedInputStream bis = new BufferedInputStream(fis);
		
//		File file1 = new File("E:\\files\\abc\\ab\\bbb.mp4");
//		FileOutputStream fos = new FileOutputStream(file1);
//		BufferedOutputStream bos = new BufferedOutputStream(fos);
		//字节写出缓冲流
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:\\files\\abc\\ab\\bbb.mp4")));
		
		byte[] b = new byte[1024];
		int l = 0;
		while((l=bis.read(b))!=-1) {
			bos.write(b, 0, l);
			bos.flush();
		}
		
		bos.close();
		fis.close();
		bis.close();
		
		long time2 = new Date().getTime();
		
		System.out.println(time2-time1);

BufferedInputStream —>字节读入缓冲流

​ 创建过程需要依赖本身的字节读入流对象

​ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(String)));

BufferedOutputStream ----->字节写出缓冲流

​ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(String)));

BufferedReader ------>字符读入缓冲流

​ BufferedReader br = new BufferedReader(new FileReader(new File(String)));

BufferedWriter -------->字符写出缓冲流

​ BufferedWriter bw = new BufferedWriter(new FileWriter(new File(String)));

		File file = new File("E:\\files\\abc\\aa\\news.txt");
		FileReader fr = new FileReader(file);
		//字符读入缓冲流
		BufferedReader br = new BufferedReader(fr);

//		String l = "";
//		while((l = br.readLine())!=null) {
//			System.out.println(l);
//		}
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File("E:\\files\\abc\\ab\\news.txt")));
		
//		使用自定义缓冲区 读写
//		char[] c = new char[10];
//		int l = 0;
//		while((l = br.read(c))!=-1) {
//			bw.write(c,0,l);
//			//bw.newLine();
//		}
		
//		整行读写
		String l = "";
		while((l = br.readLine())!=null) {
			//整行读入 以换行符为标记 并没有记录换行符 
			bw.write(l);
			//手动写出换行效果
			bw.newLine();
		}
		
		bw.close();
		fr.close();
		br.close();

练习:使用缓冲流复制一张图片 和一个文本信息

​ 文本信息有十行内容 只写出奇数行到新文件中

//		整行读写
		String l = "";
		int i = 0;
		while((l = br.readLine())!=null) {
            if(i%2==0){
                //整行读入 以换行符为标记 并没有记录换行符 
				bw.write(l);
				//手动写出换行效果
				bw.newLine();
            }
			i++;
		}
序列化流 对象流

​ 将程序中的对象持久化-------》写出操作—》序列化(字节码文件 加密)

​ 将持久化的对象转为程序内存中的临时数据------》读入操作------》反序列化(程序中可以使用的对象)

案例:

​ 将张三同学 以对象的形式持久化保存在记事本中 再反向将记事本中张三对象解析为程序中的学生对象

//要序列化对象所属的类 必须实现可序列化接口
public class Student implements Serializable{
    //添加UID标识 原始类型被修改后 依然可以反序列化
	private static final long serialVersionUID = 1L;
	
	public String name;
	public int age;
	public char gender;
    ...
        
//对象序列化操作
		Student stu = new Student("张三",18,'男');//要序列化的对象
		//序列化目标
		File file = new File("E:\\files\\abc\\ab\\stu.txt");
		//字节写出流
		FileOutputStream fos = new FileOutputStream(file);
		//对象写出流
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		//通过对象流写出对象---》序列化
		oos.writeObject(stu);
		
		oos.close();
		fos.close();   
    
//反序列化操作
		//创建对象读入流
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("E:\\files\\abc\\ab\\stu.txt")));
		
		//使用读入对象方法---》反序列化
		Object readObject = ois.readObject();
		
		Student s = (Student)readObject;
		
		System.out.println(s);

练习:

​ 保存一个ArrayList集合 实现序列化 反序列化

案例:

​ 菜单 1.注册 2.登录 3.查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值