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();
				}
			}
		}
	}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值