ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

2022.01.21_Java学习总结_IO流

2022-01-21 16:33:57  阅读:146  来源: 互联网

标签:fis Java 21 read System IO new out String


1. IO

1.1 概述

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

I : input 输入
O : output 输出

1.2 分类

·	按处理数据类型的不同,分为字节流和字符流
·	按数据流向的不同,分为输入流和输出流。(入和出是相对于内存来讲的) 

在这里插入图片描述

·	按功能不同,分为节点流和处理流
	·	节点流:直接操作数据源
	·	处理流:对其他流进行处理

1.3 四大抽象类

在这里插入图片描述

1.3.1 InputStream

在这里插入图片描述

1.3.2 OutputStream

在这里插入图片描述

1.3.3 Reader

在这里插入图片描述

1.3.4 Writer

在这里插入图片描述

1.4 文件流

1.4.1 FileInputStream

1.4.1.1 概述

用来打开文件并读取文件中的数据
想要读取一个文件,就必须要找到它
	1 绝对位置
		以系统根目录为准,比如D:/xxx\\xxx\\xx\\a.txt
	2 相对位置
		./ 表示当前目录 
		../ 表示上级目录
		../../ 上上级目录

1.4.1.2 常用方法

在这里插入图片描述

1.4.1.3 Read使用

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

	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.print ((char) data);
			}
			// data = fis.read();
			// while (data != -1) {
			// System.out.print((char) data);
			// data = fis.read();
			// }
			// 关闭资源
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	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 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 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 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 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 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 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 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 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 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 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 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 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 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());
	}

标签:fis,Java,21,read,System,IO,new,out,String
来源: https://blog.csdn.net/m0_46552030/article/details/122617927

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有