IO流技术:如何用Java实现文件拷贝功能 ?

在这里插入图片描述

四大抽象类

抽象类说明常用方法
InputStream字节输入流的父类,数据单位为字节int read()、void close()
OutputStream字节输出流的父类,数据单位为字节void write(int)、void flush()、void close()
Reader字符输入流的父类,数据单位为字符int read()、void close()
Writer字符输出流的父类,数据单位为字符void write(String)、void flush()、void close()
  • FileInputStream通过字节方式读取文件,适合所有类型的文件、图像、视频等),全字符请考虑FileReader
  • FileOutStream通过字节的方式写出或追加数据到文件,适合所有
    类型的文件,全字符请考虑FileWriter

1、理解操作流程

1、创建源
2、选择流
3、传输操作:读和写
4、释放资源

代码

public class IOTest1 {
	public static void main(String[] args){
		//1、创建源
		File src = new File("abc.txt");
		//2、选择流
		try {
			InputStream is = new FileInputStream(src);
			//3、传输操作:读
			int data1 = is.read(); //第1个数据
			int data2 = is.read(); //第2个数据
			int data3 = is.read(); //第3个数据
			int data4 = is.read(); //第4个?不是数据,文件末尾返回-1
			System.out.println((char)data1);
			System.out.println((char)data2);
			System.out.println((char)data3);
			System.out.println((char)data4);
			System.out.println(data4);
			//4、释放资源
			is.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

其中,FileInputStream是InputStream的子类。

结果

在这里插入图片描述
其中:abc.txt中的内容是 Zh2

2、规范操作

代码

public class IOTest2 {
	public static void main(String[] args){
		//1、创建源
		File src = new File("abc.txt");
		InputStream is=null;
		//2、选择流
		try {
			is = new FileInputStream(src);
			//3、传输操作:读
			int temp;
			while((temp=is.read())!=-1){
				System.out.println((char)temp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{     //考虑异常处理,加上finally
			//4、释放资源
			try {
				if(null!=is){  //没用到此资源不需要关
				is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

结果不变

3、分段读取

代码

public class IOTest3 {
	public static void main(String[] args){
		//1、创建源
		File src = new File("abc.txt");
		InputStream is=null;
		//2、选择流
		try {
			is = new FileInputStream(src);
			//3、传输操作:读
			byte[] flush = new byte[3];  //缓冲容器
			int len=-1;   //接收长度
			while((len=is.read(flush))!=-1){
				//字节数组->字符串(解码)
				String str = new String(flush,0,len);
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{     //考虑异常处理,加上finally
			//4、释放资源
			try {
				if(null!=is){  //没用到此资源不需要关
				is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

结果

在这里插入图片描述
其中,abc.txt 中的内容是 Zhoubaiqing HNU 123

4、文件字节输出流

1、创建源
2、选择流
3、操作:写出内容
4、释放资源

代码

public class IOTest4 {
   public static void main(String[] args) {
   	//1、创建源
   	File dest = new File("dest.txt");
   	//2、选择流
   	OutputStream os = null;
   	try{
   		os = new FileOutputStream(dest,true);//加参数true可以实现追加
   		//3、操作:写出内容
   		String msg = "zhoubaiqingHNU\r\n";  //结尾加\r\n可实现换行
   		//编码
   		byte[] datas = msg.getBytes();
   		os.write(datas,0,datas.length);
   		//刷新一下
   		os.flush();
   	}catch (FileNotFoundException e) {
   		e.printStackTrace();
   	} catch (IOException e) {
   		e.printStackTrace();
   	}finally{
   		//4、释放资源
   		try {
   			if (null!=os) {
   				os.close();
   			} 
   		} catch (Exception e) {
   			e.printStackTrace();
   		}
   	}
   }
}

其中,FileOutputStream是OutputStream的子类。

结果

在这里插入图片描述

5、实现文件拷贝

将之前读取输出的代码合二为一,实现文件拷贝的功能,具体代码如下。

5.1、字节文件输入输出流实现

public class Copy {
   public static void main(String[] args) {
   	copyFile("abc.txt","dest.txt");
   }
   //定义方法
   public static void copyFile(String srcPath,String destPath){
   	//1、创建源
   	File src = new File(srcPath); //源头
   	File dest = new File(destPath);  //目的地
   	//2、选择流
   	InputStream is = null;   //节点输入流
   	OutputStream os = null;  //节点输出流
   	String str = null;
   	try{
   		is = new FileInputStream(src);
   		os = new FileOutputStream(dest,false);//加参数true可以实现追加
   		//3、操作:读入与写出内容
   		byte[] flush = new byte[1024];  //缓冲容器
   		int len=-1;   //接收长度
   		while((len=is.read(flush))!=-1){
   			os.write(flush,0,len);
   		}
   		//刷新一下
   		os.flush();
   	}catch (FileNotFoundException e) {
   		e.printStackTrace();
   	} catch (IOException e) {
   		e.printStackTrace();
   	}finally{
   		//4、释放资源
   		try {
   			if (null!=os) {
   				os.close();
   			} 
   		} catch (Exception e) {
   			e.printStackTrace();
   		}
   		//分别关闭:先打开的后关闭,可以保证目的地接收数据的准确性
   		try {
   			if(null!=is){  //没用到此资源不需要关
   			is.close();
   			}
   		} catch (IOException e) {
   			e.printStackTrace();
   		}
   	}
   }
}

5.2、字符文件输入输出流

  • FileReader:通过字符的方式读取文件,仅适合字符文件
  • FileWriter:通过字节的方式写出或追加数据到文件中,仅适合
    字符文件

输入流

Reader reader=null;
		try {
			reader = new FileReader(src);
			//3、传输操作:读
			char[] flush = new char[1024];  //缓冲容器
			int len=-1;   //接收长度
			while((len=reader.read(flush))!=-1){
				//不用再解码
				String str = new String(flush,0,len);
				System.out.println(str);
			}
		} 

输出流

//2、选择流
		Writer writer = null;
		try{
			writer = new FileWriter(dest,true);//加参数true可以实现追加
			//3、操作:写出内容
			//写法一:
			String msg = "周百青HNU\r\n";  //结尾加\r\n可实现换行
			char[] datas = msg.toCharArray();//字符串到字符数组
			writer.write(datas,0,datas.length);
			//写法二:
			String msg = "周百青HNU\r\n";
			writer.write(msg);
			//写法三:
			writer.append("\r\n周百青\r\n").append("HNU");
			//刷新一下
			writer.flush();
		}

我们只需修改很少的内容,就可以用字符输入输出流,来实现文件拷贝的功能。只需要合并一下,具体不再赘述。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值