Java中的IO流

Java IO流详解
一、 什么是IO流

一般情况下是按照当前程序使用的内存为参照物来考虑数据的走向问题。
以文件操作为例
从内存中保存数据到硬盘 output
从硬盘中读取数据到内存 input

看视频,缓冲
使用缓冲可以让用户体验提高,相对来说较为平和的观看体验。
网页第一次访问时,加载时间较慢,第二次打开,速度很快

IO流按照流向分类可分为输入输出,按照文件操作处理单元分类可分为字节流字符流

FileInputStream 文件操作输入字节流
FileOutputStream 文件操作输出字节流

FileReader 文件操作输入字符流
FileWriter 文件操作输出字符流

二、文件操作字节流
1、文件操作输入字节流 FileInputStream
1.1 Constructor 构造方法
FileInputStream(File file);
	这里是根据提供的File类对象创建对应的文件操作输入字节流。
FileInputStream(String pathName);	
	这里是根据提供的String类型文件路径,创建对应的文件操作输入字节流。

都会抛出 FileNotFoundException 文件未找到异常。

1.2 Method 成员方法
int read();
	读取文件中的一个字符数据,通过返回值返回,返回值类型是int类型,但是在int类
	型中有且只有低16位数据有效
int read(char[] arr);
	读取文件中的数据保存到字符数组中,返回值类型是读取到的字符个数
int read(char[] arr, int offset, int len);
	读取文件中的数据保存到字符数组中,要求从数组中下标offset开始,到len结束,返
	回值类型是读取到的字符个数

以上方法,如果读取到文件默认,返回值为-1 EOF End Of File
如果读取操作工作中出现问题则抛出异常IOException

1.3 文件读取使用缓冲和非缓冲差距
	public static void readTest() {
		// 确定操作文件
		File file = new File("C:\\aaa\\1.txt");
		// 字节输入流读取文件对象
		FileInputStream fileInputStream = null;	
		try {
			// 根据File类对象创建对应的字节输入流
			fileInputStream = new FileInputStream(file);	
			// 准备一个8KB字节缓冲数组
			byte[] buf = new byte[1024 * 8];
			int length = -1;
			// 读取数据
			while ((length = fileInputStream.read(buf)) != -1) {
				System.out.println(new String(buf, 0, length));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 在finally代码块中关闭资源
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

内存的运作速度是远高于硬盘的速度的。
以上代码中,使用缓冲之后,从硬盘中一口气读取8KB数据存储在内存中,供程序使用。
如果是一个字节一个字节读取,CPU取出4KB数据,结果有4095无效。

2、文件操作输出字节流
2.1 Constructor 构造方法
FileOutputStream(File file);
	根据File类对象创建对应的文件输出字节流对象
FileOutputStream(String pathName);
	根据String类型文件路径创建对应的文件输出字节流对象

以上两个构造方法,创建的FileOutputStream是删除写操作,会将原文件中的内容全部删除之后,写入数据。

FileOutputStream(File file, boolean append);
	根据File类对象创建对应的文件输出字节流对象。创建对象时给予append参数为
	true,表示追加写。
FileOutputStream(String pathName, boolean append);
	根据String类型文件路径创建对应的文件输出字节流对象,创建对象时给予append参
	数为true,表示追加写。

FileOutputStream构造方法是拥有创建文件的内容,如果文件存在则不创建。文件不存在且路径正确,创建对应文件。否则抛出异常FileNotFoundException

2.2 Method 成员方法
void write(int b);
	写入一个字节数据到当前文件中,参数是int类型,但是有且只会操作对应的低八位数
	据
void write(byte[] buf);	
	写入字节数组中的内容到文件中
void write(byte[] buf, int offset, int length);	
	写入字节数组中的内容到文件中,从指定的offset开始,到指定长度length

以上方法会抛出异常:IOException

2.3 使用方法演示
	public static void writeTest2() {
		// 确定文件
		File file = new File("C:/aaa/test.txt");
		// 文件操作字节输出流对象
		FileOutputStream fileOutputStream = null;
		try {
			// 创建FileOutputStream 
			fileOutputStream = new FileOutputStream(file);
			// 准备byte类型数组
			byte[] arr = {65, 66, 67, 68, 69, 70, 71};
			fileOutputStream.write(arr, 2, 3);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
三、文件操作字符流
1、文件操作输入字符流
1.1 Constructor 构造方法
FileReader(File file)
	根据File类对象创建对应的FileReader字符流输入对象
FileReader(String pathName)
	根据String类型文件路径创建对应的FileReader字符流输入对象

如果文件不存在,抛出异常FileNotFoundException

1.2 Method 成员方法
int read();
	读取文件中的一个字符数据,通过返回值返回,返回值类型是int类型,但是在int类
	型中有且只有低16位数据有效
int read(char[] arr);
	读取文件中的数据保存到字符数组中,返回值类型是读取到的字符个数
int read(char[] arr, int offset, int len);
	读取文件中的数据保存到字符数组中,要求从数组中下标offset开始,到len结束,返
	回值类型是读取到的字符个数

以上方法,如果读取到文件默认,返回值为-1 EOF End Of File
如果读取操作工作中出现问题则抛出异常IOException

1.3 使用方法演示
	public static void readTest2() {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(new File("C:/aaa/3.txt"));
			
			char[] buf = new char[1024 * 4];
			int length = -1;
			
			while ((length = fileReader.read(buf)) != -1) {
				System.out.println(new String(buf, 0, length));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
2、文件操作输出字符流
2.1 Constructor 构造方法
FileWriter(File file);
	根据File类对象创建对应文件的文件操作输出字符流
FileWriter(String pathName);
	根据String类型文件路径创建对应文件的文件操作输出字符流
FileWriter(File file, boolean append);
	根据File类对象创建对应文件的文件操作输出字符流,并要求为追加写
FileWriter(String pathName, boolean append);
	根据String类型文件路径创建对应文件的文件操作输出字符流,并要求为追加写

如果创建FileWrite对象时,这里文件不存在,路径合法则会创建对应的操作文件,如果路径不合法则会抛出异常 FileNotFoundException

2.2 Method 成员方法
void write(int ch);
	写入一个char类型数据到文件中
void write(char[] arr);
	写入一个char类型数组到文件中
void write(char[] arr, int offset, int length);	
	写入一个char类型数组到文件中,要求从offset下标位置开始读取数组数据,长度为
	length
void write(String str);
	写入一个字符串到文件中
void write(String str, int offset, int lenght);
	写入一个字符串到文件中,要求从指定字符串offset下标位置开始,长度为length

如果写入数据操作过程中发生问题则会抛出异常 IOException

2.3 字符流文件拷贝
	public static void main(String[] args) {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		
		try {
			fileReader = new FileReader(new File("D:/aaa/logo.jpg"));
			fileWriter = new FileWriter(new File("D:/aaa/temp.jpg"));
			
			char[] buf = new char[1024 * 4];
			int length = -1;
			
			while ((length = fileReader.read(buf)) != -1) {
				fileWriter.write(buf, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
### Java IO的概念及使用方法 JavaIO通过`java.io`包下的类和接口来支持,主要用于数据的读取和写入操作。IO可以分为输入(InputStream)和输出(OutputStream),并且根据处理的数据单位不同,又可分为字节和字符[^1]。 #### 1. IO的分类 - **字节**:以字节为单位进行数据处理,适用于所有类型的文件,包括二进制文件。主要类有`FileInputStream`和`FileOutputStream`。 - **字符**:以字符为单位进行数据处理,仅适用于纯文本文件。主要类有`FileReader`和`FileWriter`[^4]。 此外,JavaIO还采用了装饰者设计模式,将分为底层节点和上层处理。节点直接与物理存储节点关联,而处理则对节点进行包装,提供额外的功能,如缓冲、过滤等[^1]。 #### 2. 字符的使用示例 以下是一个使用字符读取文件内容的示例代码: ```java import java.io.BufferedReader; import java.io.FileReader; public class Demo2 { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("E:\\Test.txt")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述代码中,`BufferedReader`被用来提高读取效率,并且通过`readLine()`方法逐行读取文件内容[^2]。 #### 3. 序列化的使用 序列化是Java IO的一个重要应用,用于将对象转换为字节以便于存储或传输。为了实现序列化,类必须实现`Serializable`接口。以下是一个对象序列化的示例代码: ```java import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Student implements Serializable { private static final long serialVersionUID = 1L; String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } } public class SerializationDemo { public static void main(String[] args) throws Exception { Student student = new Student("张三", 22); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student")); oos.writeObject(student); oos.flush(); oos.close(); } } ``` 在此示例中,`ObjectOutputStream`被用来将`Student`对象序列化并保存到文件中[^3]。 #### 4. 关闭的重要性 如果不关闭IO,可能会导致资源泄漏,影响程序性能甚至引发异常。因此,在完成数据读写后,务必调用`close()`方法释放相关资源[^1]。 --- ###
评论 11
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值