Day15 学习java(IO流)

Day15 学习java(IO流),第1张

Day15 学习java(IO流) IO 概述

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

I :input输入
O:output 输出

分类

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

文件,其他输入设备、内存 ----输入流----→内存----输出流----→文件,其他输入设备、内存

按功能不同,分为节点流和处理流
节点流:直接 *** 作数据源
处理流:对其他流进行处理 四大抽象类

InputStream


OutputStream


Reader


Writer


文件流 FileInputStream 概述

用来打开文件并读取文件中的数据,想要读取一个文件,就必须要找到它
绝对位置:D:/xxx
相对位置:./表示当前目录,…/表示上级目录

常用方法

Read使用 文件B.txt

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

public class IO_01 {

	public static void main(String[] args) {
		// 绝对路径
		// FileInputStream fis = new FileInputStream("D:/a.txt");
		// 相对路径
		try (FileInputStream fis = new FileInputStream("./src/B.txt")) {
			// 自动关闭资源
			// read:读取一个字节,并返回对应的ASCII码值,返回为int类型,如果到达文件末尾(读完了),就返回-1
			int data = 0;
			while ((data = fis.read()) != -1) {
				System.out.print((char) data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Read重载使用

read方法重载:可以传递一个数组,一次读取会把该数组读满/读完,然后一次性返回

返回int 类型,为当前读取的个数,如果达到文件末尾 返回-1

数组就相当于一个缓冲区,效率会有所提升

public class IO_04 {

	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 (IOException e) {
			e.printStackTrace();
		}
	}

}

FileReader 概述

FileReader:一次读取一个字符,也就是两个字节,主要用于读取纯文本文件,解决乱码问题

read():一次读取一个字符,返回对应的ASCII码,达到文件末尾返回-1

read(char[]):一次读取一个字符数组,提高读取效率,返回本次读取的字符个数,到达文件末尾返回-1

使用方式
public class IO_05 {

	public static void main(String[] args) {
		try (FileInputStream fis = new FileInputStream("./src/B.txt")) {
			int temp = 0;
			while ((temp = fis.read()) != -1) {
				System.out.print((char) temp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
public class IO_06 {

	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.println(new String(chars, 0, temp));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		;
	}

}

FileOutputStream 概述

FileOutputStream是字节输出流,用于把数据写出到指定文件中

如果指定文件不存在,会自动创建该文件,但是不会创建目录(文件夹)

常用方法

构造方法

覆盖式写入

FileOutputStream fos = new FileOutputStream(filePath);

追加式写入

FileOutputStream fos = new FileOutputStream(filePath,true);
使用方式

覆盖式写入
写之前

public class IO_07 {

	public static void main(String[] args) {
		try (FileOutputStream fos = new FileOutputStream("D:/a.txt")) {
			// 写出对应的ASCII码
			fos.write(97);

			byte[] bytes = { 97, 98, 98, 98, 98, 99 };
			fos.write(bytes);
			// 不能直接写字符串
			String str = "你好吗";
			// getBytes:把字符串转换称为字节数组
			fos.write(str.getBytes());
			// 刷新缓存,强制持久化
			fos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

写之后

--------------------------------------------------------------------------------------------------------------------
继续上面追加式写入

public class IO_08 {

	public static void main(String[] args) {
		try (FileOutputStream fos = new FileOutputStream("D:/a.txt", true)) {
			String str = "新写";
			fos.write(str.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

FileWriter

用法和字节输出一致,只不过新增了可以写出字符串的方法重载

public class IO_09 {

	public static void main(String[] args) {
		// 用法和字节输出一致,只不过新增了可以写出字符串的方法重载
		try (FileWriter fw = new FileWriter("D:/a.txt",true)) {
			// 字符数组
			char[] chars = { 'a', 'b' };
			fw.write(chars);
			// 可以直接写出字符串
			fw.write("还好吗");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

缓冲流


特点
 主要是为了提高效率而存在的,减少物理读取次数
 提供readLine()、newline()这样的便捷的方法(针对缓冲字符流)
 在读取和写入时,会有缓存部分,调用flush为刷新缓存,将内存数据写入到磁盘

BufferedReader
public class IO_10 {

	public static void main(String[] args) {
		try (// 创建字符输入流
		FileReader fr = new FileReader("./src/B.txt");
				// 创建字符输入缓冲流对象
				BufferedReader br = new BufferedReader(fr);) {
			//br.readLine():读取一行数据,返回读取到的这一行数据的内容,为String,到达文件末尾返回null
			String temp = null;
			while((temp = br.readLine())!=null){
				System.out.println(temp);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
BufferedWriter
public class IO_11 {

	public static void main(String[] args) {
		try (// 创建字符输出流
		FileWriter fw = new FileWriter("D:/a.txt", true);
				// 创建字符输出缓冲流
				BufferedWriter bw = new BufferedWriter(fw);) {

			bw.newline();
			// 写出
			bw.write("你好吗好吗");
			// 换行
			bw.newline();
			bw.write("你管得着吗");
			// 刷缓存
			bw.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

转换流


特点
 转换流是指将字节流字符流的转换,主要有InputStreamReader和OutputStreamWriter
 InputStreamReader:主要将字节流输入流转换成字符输入流
 OutputStreamWriter:主要是将字节流输出流转换成字符输出流

public class IO_12 {

	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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

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

打印流 概述

特点
 打印流是输出最方便的类
 PrintStream是OutputStream的子类,把一个输出流的实例传递到打印流之后,可以更加方便地输出内容,相当于把输出流重新包装一下
 PrintStream类的print()方法被重载很多次print(int i ),print(boolean b)等
【示例】
使用StringReader读取字符串,然后使用打印流打印到控制台
【标准输入/输出】
Java的标准输入/输出分别通过System.in和System.out来代替,在默认的情况下分别代表键盘和显示器,当程序通过System.in来获得输入时,实际上是通过键盘获得输入。当程序通过System.out执行输出时,程序总是输出到屏幕

使用方式
public class IO_13 {

	public static void main(String[] args) throws FileNotFoundException {
		// 创建输出流
		FileOutputStream fos = new FileOutputStream("D:/a.txt");
		// 封装为打印流
		PrintStream ps = new PrintStream(fos);
		ps.println(122);
		ps.println("sadasdaasd");
		// System中的打印流,默认打印到控制台
		System.out.println("---");
		// 更改System的打印流
		System.setOut(ps);
		// 以下所有打印 *** 作,不再显示在控制台中,而是打印到指定文件中
		System.out.println("还好");
		System.out.println("一般");
	}

}


数据流 概述

数据流 是为了不同平台之间数据读取的统一性,Linux Windows等 *** 作系统对数据进行存储的时候方式是不同的

为了解决之间的差异性,java提供了两个轻量级的方法,只要每个平台有java环境,就能保证数据的一致性

使用方式
public class IO_14 {

	public static void main(String[] args) throws Exception {
		// 创建对象
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(
				"D:/a.txt"));
		// 写出数据
		dos.writeByte(1);
		dos.writeInt(152);
		dos.writeBoolean(false);
		dos.writeUTF("还好");
		dos.flush();
		dos.close();
	}

}

此时查看a.txt

再读

public class IO_15 {

	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.readInt());
		System.out.println(dis.readBoolean());
		System.out.println(dis.readUTF());
	}

}


一定要保证顺序一致

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5709916.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存