Java面试复习(3)之IO流

java面试复习(1)
java面试复习(2)之File类
Java面试复习(3)之IO流
java面试复习(4)之字符流
File类:
英语描述一个文件或者文件夹的。

通过File对象我们可以读取文件或者文件夹的属性数据,如果我们需要读取文件的内容数据,那么我们需要使用IO流技术。

IO流(Input Output)

IO解决问题:解决设备与设备之间的数据传输问题。内存—>硬盘 硬盘—>内存

IO流分类:

如果是按照数据的流向划分:
判断使用输入流还是输出流的依据:以当前程序作为参照物,观察数据是流入还是流出,如果是流出则使用输出流,如果是流入,则使用输入流。

如果按照处理的单位划分:

字节流:字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。

字符流:字符流读取的数据是以字符为单位的。字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能识别的字符。

字符流=字节流+解码

输入字节流:

---------|InputStream所有输入字节流的基类 抽象类

-------------|FileInputStream 读取文件数据的输入流字节

使用FileInputStream读取文件数据的步骤:

1.找到目标文件

2.建立数据的输入通道

3.读取文件中的数据

4.关闭资源
package nyist.net;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo5 {
public static void main(String[] args) throws IOException{
		
	readTest4();
	
}
//读取的方式一:
//缺陷:无法读取完整一个文件的数据
public static void readTest1() throws IOException{
	//1.找到目标文件:
	File file = new File("E:\\a.txt");
	//2.建立数据的输入通道
	FileInputStream fileInputStream = new FileInputStream(file);
	//3.读取文件的数据
	int content = fileInputStream.read();
	System.out.println("读到的数据是:"+(char)content);
	//关闭资源,实际就是释放资源
	fileInputStream.close();
}

//读取方式二:使用循环读取文件的数据
public static void readTest2() throws IOException{
	//1.找到目标文件:
	File file = new File("E:\\a.txt");
	//2.建立数据的输入通道
	FileInputStream fileInputStream = new FileInputStream(file);
	//3.读取文件的数据
	int content = 0;//声明该变量用于存储读取到的数据
	while((content = fileInputStream.read())!=-1){
		
		System.out.println((char)content);
			
	}
	//4.关闭资源
	fileInputStream.close();
}
//读取方式3:使用缓冲数组读取。
//缺点:无法读取完整一个文件的数据,比如说一个文件很大。
public static void readTest3() throws IOException{
	
	//1.找到目标:
	File file  = new File("E:\\a.txt");
	//2.建立数据的输入通道
	FileInputStream fileInputStream = new FileInputStream(file);
	//3.建立缓冲字节数组,读取文件的数据.
	byte[] buf = new byte[1024];//相当于超市里面的购物车.
	int length = fileInputStream.read(buf);
	//如果使用read读取数据传入字节数组,那么数据式存储到字节数组中的,
	//而这时候read方法的返回值时表示的是本次读取了几个字节数据到字节数组中。
	System.out.println("length"+length);
	//使用字节数组构建字符串
	String content = new String(buf,0,length);
	System.out.println("内容"+content);
	//关闭资源
	fileInputStream.close();
}

//方式4:使用缓冲数组配合循环一起读取。
public static void readTest4() throws IOException{
	
	//1.找到目标文件
	File file  = new File("E:\\a.txt");
	//2.建立数据的疏通管道
	FileInputStream fileInputStream = new FileInputStream(file);
	//建立缓冲数组配合循环读取文件的数据
	int length = 0;//保存每次读取到的字节个数。
	byte[] buf = new byte[1024];//存储读取到的数据
	//缓冲数组的长度一般是1024的倍数,因为与计算机的处理单位。理论上缓冲数组越大,效率越高.
	while((length = fileInputStream.read(buf))!=-1){
		System.out.println(new String(buf,0,length));
	}
	//关闭资源
	fileInputStream.close();
}
}

输出字节流:
-----------|OutputStream是所有输出字节流的父类。抽象类。
-----------------|FileOutStream向文件输出数据的输出字节流

FileOutputStream使用:

	1.找到目标文件

	2.建立数据的输出通道

	3.把数据转换成字节数组写出。

	4.关闭资源。

FileOutputStream要注意细节:

1.使用FileOutputStream的时候,如果目标文件不存在,那么会自动创建目标文件。

2.使用FileOutputStrteam写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数。

3.使用FileOutputStream写数据的时候,如果目标文件已经存在,需要在原来的数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。

4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。


```java

```java
	package nyist.net;
	import java.io.File;
	import java.io.FileNotFoundException;
	import java.io.FileOutputStream;
	import java.io.IOException;
	public class Demo6 {
	public static void main(String[] args) throws IOException {
		
		//writeTest();
		  writeTest1();
		
	}
	//使用字节数组把数据写出
	public static void writeTest() throws IOException{
		//找到目标文件
		File file = new File("E:\\b.txt");
		//建立数据通道
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		//把数据写出
		String data = "abcde";
		fileOutputStream.write(data.getBytes());
		//关闭资源
		fileOutputStream.close();
	}
	public static void writeTest1() throws IOException{
		
		File file = new File("E:\\b.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file,true);
		String data = "abc";
		byte [] buf = data.getBytes();
		fileOutputStream.write(buf, 0, 3);
		//从字节数组的指定索引值开始写。
		fileOutputStream.close();
	}
	}

//需求:拷贝图片

public class Demo7 {

public static void main(String[] args) throws IOException {
	
	copyImage();
	
}

public static void copyImage() throws IOException{
	
	File file1 = new File("C:\\Users\\LENOVO\\Pictures\\Camera Roll\\假装生活在1994.png");
	File file2 = new File("E:\\假装生活在1994.png");
	FileInputStream fileInputStream = new FileInputStream(file1);
	FileOutputStream fileOutputStream = new FileOutputStream(file2);
	int length = 0;
	byte [] buf = new byte[1024];
	while((length = fileInputStream.read(buf))!=-1){
		fileOutputStream.write(buf, 0, length);
	}
	fileOutputStream.close();
	fileInputStream.close();
}
}

IO处理异常

首先复习一下异常的基本知识:

try { //执行的代码,其中可能有异常。一旦发现异常,则立即跳到catch执行。否则不会执行catch里面的内容 }

catch { //除非try里面执行代码发生了异常,否则这里的代码不会执行 }

finally { //不管什么情况都会执行,包括try catch 里面用了return ,可以理解为只要执行了try或者catch,就一定会执行 finally }

public static void readStream() {
		
	FileInputStream fileInputStream = null;
	try {
	//找到目标:
	File file = new File("F:\\a.txt");
	//建立数据疏通管道

		fileInputStream = new FileInputStream(file);
		byte[] buf = new byte[1024];
		int length = 0;
		while((length = fileInputStream.read(buf))!=-1){
				System.out.println(new String(buf,0,length));
		}
		
	} 
	//建立缓冲数组读取数据
							
catch (IOException e) {
	 	/*
	 	 *处理的代码。。首先你要组织后面的代码,而且需要通知调用者这里出错了。。。
	 	 *throw new RuntimeException(e);
	 	 *把IOException传递给RunTimeException包装一层,然后再抛出,
	 	 *这样子做的目的是为了让调用者使用变得更加灵活。
	 	 *
	 	 * */
 		System.out.println("读取文件资源出错...");
		throw new RuntimeException(e);
	}finally {
		if(fileInputStream!=null){
			try {
				fileInputStream.close();
				System.out.println("关闭资源成功...");
			} catch (IOException e) {
				System.out.println("关闭资源失败...");	
				throw new RuntimeException(e);
			}
		}	
	}

Buffered

缓冲输入字节流对象,更高效读取文件。

输入字节流体系:

---------|InputStream 输入字节流的基类。抽象。

------------|FileInputStream读取文件数据的输入字节流。

------------|BufferedInputStream缓冲输入字节流

缓冲输入字节流的出现主要为了提高读取文件数据的效率

其实该类内部只不过是维护了一个8kb的字节数组而已。

注意:凡是缓冲流都不具备读写文件的能力。

使用BufferedInputStream的步骤:

1.找到目标文件

2.建立数据的输入通道

3.建立缓冲输入字节流

4.关闭资源

输入字节缓冲流例子

package nyist.net;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo8 {
public static void main(String[] args) throws IOException {
	readTest1();
}

public static void readTest1() throws IOException{
	
	File file = new File("E:\\a.txt");
	FileInputStream fileInputStream = new FileInputStream(file);
	BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
	int content = 0;
	while((content = bufferedInputStream.read())!=-1){
		System.out.println((char)content);
	}
	bufferedInputStream.close();
	//调用BufferedInputStream的close方法实际上关闭的是FileinputStream。
}
}

输出字节缓冲流

----------|OutputStream

--------------|FileOutputStream向文件输出数据的输出字节流。

--------------|BufferedoutputStream缓冲输出字节流

BufferedOutputStream出现的目的是为了提高写数据的效率。

使用BufferedOutputStream要注意的细节:

1.使用BufferedOutputStream写数据的时候,它的write方法是先把数据写到它内部维护的字节数组中。

2.使用BufferedOutputStream写数据的时候,它的write方法是先把数据写到它内部维护的字节数组中,如果需要把数据真正的写道硬盘上面,需要调用flush方法或者是close方法,或者是内部维护的字节数组已经填满数据的时候。

public class Demo2 {
public static void main(String[] args) throws IOException {
	//找到目标文件
	File file = new File("F:\\a.txt");
	//建立数据的输出通道
	FileOutputStream  fileOutputStream = new FileOutputStream(file);
	//建立缓冲输出字节流对象
	BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
	//把数据写出
	bufferedOutputStream.write("hello world".getBytes()); 
	//把缓冲数组中内部的数据写到硬盘上面。
	//bufferedOutputStream.flush();
	bufferedOutputStream.close();
}
}

练习:使用缓冲输入输出字节流拷贝一个图片

package nyist.net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo9 {
public static void main(String[] args) throws IOException {
	copyImage();
}
public static void copyImage() throws IOException{
	
	File file1 = new File("C:\\Users\\LENOVO\\Pictures\\Camera Roll\\假装生活在1994.png");
	File file2 = new File("E:\\假装生活在1994.png");
	FileInputStream fileInputStream = new FileInputStream(file1);
	FileOutputStream fileOutputStream = new FileOutputStream(file2);
	BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
	BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
	
	int content = 0;
	while((content = bufferedInputStream.read())!=-1){
		bufferedOutputStream.write(content);
	}
		bufferedOutputStream.close();
		bufferedInputStream.close();
}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值