Day_19 IO流

1. IO

1.1 概述

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

I : input 输入

O : output 输出

1.2 分类

按处理数据类型的不同,分为字节流和字符流

按数据流向的不同,分为输入流和输出流。(入和出是相对于内存来讲的)

按功能不同,分为节点流和处理流

节点流:直接操作数据源

处理流:对其他流进行处理

1.3 四大抽象类

1.3.1 InputSteam

 

1.3.2 OutputStream

 1.3.3 Reader

 1.3.4 Writer

 

 1.4 文件流

1.4.1 FileInputStream

用来打开文件并读取文件中的数据

想要读取一个文件,就必须要找到它

1 绝对位置

以系统根目录为准,比如D:/xxx\\xxx\xx\a.txt

2 相对位置

./ 表示当前目录

../ 表示上级目录

../../ 上上级目录

1.4.1.2 常用方法

 1.4.1.3 Read 使用

read : 读取一个字节,并返回对应的ASCII码值,返回为int类型,如果到达文件末尾(读完了) 则返回-1

/**
 * 字节输入流,按照字节的方式进行数据读取
 * 
 * @author 天亮教育-刘世龙
 * @Date 2022年1月21日 下午7:55:01
 */
public class IO_01_FileInputStream_01 {
public static void main(String[] args) {
	//创建字节输入流
	//传入文件路径,用于打开该文件获取数据
	//绝对路径
	//FileInputStream fis = new FileInputStream("D:\\a.txt");
	//在eclipse中./定位当前项目,并不是当前文件
	 //相对路径
	try{
		FileInputStream fis = new FileInputStream("./src/b.txt");
		//read:读取一个字节,并返回对应的ASCII码值,返回为int类型,如果达到文件末尾(读完了)则返回-1
		//int data = fis.read();
		int data = 0;
		while((data = fis.read())!= -1){
			System.out.println((char)data);
		}
		// data = fis.read();
		//while(data!=-1){
		//System.out.println((char)data);
		//data = fis.read();
		//}
		//关闭资源
		fis.close();	
	}catch(IOException e){
		e.printStackTrace();
	}
}
}
/**
 * 字节输入流,按照字节的方式进行数据读取
 */
public class IO_02_FileInputStream_2 {
	public static void main(String[] args) {
		// 创建字节输入流
		FileInputStream fis = null;
		try {
			 fis = new FileInputStream("./src/b.txt");
			// read : 读取一个字节,并返回,返回为int类型,如果到达文件末尾(读完了) 则返回-1
			// int data = fis.read();
			int data = 0;
			while ((data = fis.read()) != -1) {
				System.out.print ((char) data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
/**
 * 字节输入流,按照字节的方式进行数据读取
 */
public class IO_03_FileInputStream_3 {
	public static void main(String[] args) {
		// 自动关闭资源
		// 创建字节输入流
		try (FileInputStream fis = new FileInputStream("./src/b.txt");) {
			// read : 读取一个字节,并返回,返回为int类型,如果到达文件末尾(读完了) 则返回-1
			// int data = fis.read();
			int data = 0;
			while ((data = fis.read()) != -1) {
				System.out.print((char) data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

1.4.1.4 Read 重载使用

/**
 * read方法重载 : 可以传递一个数组,一次读取会把该数组读满/读完,然后一次性返回
 * 
 * 返回int类型,为当前读取的个数,如果达到文件末尾 返回-1
 * 
 * 数组就相当于是一个缓冲区,效率会有所提升
 * 
 */
public class IO_04_FileInputStream_4 {
	public static void main(String[] args) {
		try(FileInputStream fis = new FileInputStream("./src/b.txt")) {
			// available : 可读取的字节数
			System.out.println(fis.available());
			// 数组容量并不是越大效率就越高,容量和数据大小刚好一致的时候,效率最高
			byte[] bytes = new byte[fis.available()];
			int temp = 0;
			while (( temp = fis.read(bytes)) != -1) {
				// 把字节数组转换为字符串,可能出现数据重复问题
				// System.out.print(new String(bytes));
				// 因为read返回的是当前次读取的元素个数,所以我们可以指定读多少,转换多少
				// 数组 , 起始位置(包含) , 个数
				 System.out.print(new String(bytes,0,temp));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.4.2 FileReader

1.4.2.1 概述

/**
 * FileReader : 一次读取一个字符,也就是两个字节,主要用于读取纯文本文件,解决乱码问题
 * 
 * read() : 一次读取一个字符,返回对应的ASCII码,达到文件末尾返回-1
 * 
 * read(char[]) : 一次读取一个字符数组,提高读取效率,返回本地读取的字符个数,到达文件末尾返回-1
 */

1.4.2.2 使用方式

public class IO_05_FileReader_1 {
	public static void main(String[] args) {
		try (FileReader fr = new FileReader("./src/b.txt");){
			int temp = 0;
			while ((temp = fr.read()) != -1) {
				System.out.print((char)temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
public class IO_06_FileReader_2 {
	public static void main(String[] args) {
		try (FileReader fr = new FileReader("./src/b.txt");){
			int temp = 0;
			char[] chars = new char[10];
			while ((temp = fr.read(chars)) != -1) {
				System.out.print(new String(chars,0,temp));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.4.3 FileOutputStream

1.4.3.1 概述

/**
 * FileOutputStream是字节输出流,用于把数据写出到指定文件中
 * 
 * 如果指定文件不存在,会自动创建该文件,但是 不会创建目录(不会创建文件夹)
 * 
 */

1.4.3.2 常用方法

 1.4.3.3 构造方法

public class IO_08_FileOouputStream_2 {
	public static void main(String[] args) throws IOException {
		// 覆盖写入,当创建该文件的输出流对象的时候,就会把该文件中内容全部清除
		// FileOutputStream fos1 = new FileOutputStream("D:/a.txt");
		
		// 创建对象时,传入第二个参数 true 说明是追加写入,则不会清空该文件
		FileOutputStream fos1 = new FileOutputStream("D:/a.txt",true);
		fos1.write(98);
	}
}

1.4.3.4 使用方式

public class IO_07_FileOutputStream_1 {
	public static void main(String[] args) {
		try (FileOutputStream fos = new FileOutputStream("D:/a.txt");) {
			// 写出对应的ASCII码
			fos.write(97);
			byte[] bytes = {97,98,99};
			// 写出数组
			fos.write(bytes);
			
			// 不能直接写出字符串
			String str = "你好吗";
			// getBytes : 把该字符串转换为字节数组
			fos.write(str.getBytes());
			// 刷新缓存,强制持久化
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

1.4.4 FileWrite

public class IO_09_FileWriter {
	public static void main(String[] args) {
		// 用法和字节输出 一致  只不过新增了可以写出字符串的方法重载
		try (FileWriter fw = new FileWriter("D:/a.txt")){
			// 写出字符数组
			char[] chars = {'a','c'};
			fw.write(chars);
			
			// 可以直接写出字符串
			fw.write("你好吗?");
			
			fw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.5 缓冲流

 【特点】

主要是为了提高效率而存在的,减少物理读取次数

提供readLine()、newLine()这样的便捷的方法(针对缓冲字符流)

在读取和写入时,会有缓存部分,调用flush为刷新缓存,将内存数据写入到磁盘

1.5.1 BufferedReader

/**
 * 字符输入缓冲流,为了解决效率问题
 */
public class IO_10_BufferedReader {
	public static void main(String[] args) {
		try (
		// 创建字符输入流
		FileReader fr = new FileReader("./src/day_01/Test_01.java");
				// 创建字符输入缓冲流对象
				BufferedReader br = new BufferedReader(fr);) {
			// br.readLine() : 读取一行数据,返回读取到的这一行数据的内容,为String , 到达文件末尾返回 null
			String temp = null;
			while ((temp = br.readLine()) != null) {
				System.out.println(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.5.2 BufferedWriter

public class IO_11_BufferedWriter {
	public static void main(String[] args) {
		try (
		// 创建字符输出流
		FileWriter fw = new FileWriter("D:/a.txt");
				// 创建字符输出缓冲流
				BufferedWriter bw = new BufferedWriter(fw);) {
			// 写出
			bw.write("你好吗");
			// 换行
			bw.newLine();
			bw.write("你管得着吗");
			// 刷缓存
			bw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.6 转换流

 

// 转换流
public class IO_12_InputStreamReader {
	public static void main(String[] args) {
		try (FileInputStream fis = getInputStream("D:/a.txt");
		// 使用转换流把字节输入流转换为字符输入流
				InputStreamReader isr = new InputStreamReader(fis);) {
			int temp = 0;
			while ((temp = isr.read()) != -1) {
				System.out.print((char) temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 需求 : 根据文件路径,获取文件的输入流对象
	public static FileInputStream getInputStream(String filePath)
			throws FileNotFoundException {
		return new FileInputStream(filePath);
	}
}

1.7 打印流

1.7.1 概述

 1.7.2 使用方式

//	打印流 就是输出流


public class IO_13_PrintStream {
	public static void main(String[] args) throws Exception{
		// 创建输出流
		FileOutputStream	fos = new FileOutputStream("D:/a.txt");
		// 封装为打印流
		PrintStream ps= new PrintStream(fos);
		ps.println(123);
		ps.println("asdasd阿萨德");
		
		// System中的打印流 默认打印到控制台
		System.out.println("xxxx");
		// 更改System中的打印流
		System.setOut(ps);
		
		// 以下所有打印操作,不再显示在控制台中,而是打印到指定文件中
		System.out.println("xxxxxxxxx");
		System.out.println("你好吗");
	}
}

1.8 数据流

1.8.1 概述

/**
 * 数据流 是为了不同平台之间数据读取的统一性,Linux,Windows等操作系统对数据进行存储的时候方式是不同的
 * 
 * 为了解决之间的差异性,java提供了两个轻量级的方法,只要每个平台有java环境,就能保证数据的一致性
 * 
 * 比如 在进行网络协议传输的时候,是非常适用的
 */

1.8.2 使用

public class IO_14_DataOutputStream {
	public static void main(String[] args)  throws Exception{
		// 创建对象
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:/a.txt"));
		// 写出数据
		dos.writeByte(1);
		dos.writeShort(12);
		dos.writeInt(51);
		dos.writeUTF("");
		
		dos.flush();
		dos.close();
	}
}
public class IO_15_DataInputStream {
	public static void main(String[] args) throws Exception {
		DataInputStream dis = new DataInputStream(new FileInputStream(
				"D:/a.txt"));

		// 必须保证 顺序和类型一致
		System.out.println(dis.readByte());
		System.out.println(dis.readShort());
		System.out.println(dis.readInt());
		System.out.println(dis.readUTF());
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值