hpe实训课(IO-字符流)

FileReader和FileWriter 处理字符流(文本文件)

1.读取一个文件的内容
public void testFileReader() {
		FileReader fr = null;
		try {
			// 1.准备好一个读入的文件
			File file = new File("oop.txt");
			// 2.创建一个基本字符流的FileReader对象
			fr = new FileReader(file);
			// 3.读数据
			// 存放每次读取的字符
			char[] c = new char[24];
			int len;
			while ((len = fr.read(c)) != -1) {
				// 每一次读取长度为len的字符,从索引为0的位置开始读
				String str = new String(c, 0, len);
				System.out.print(str);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			try {
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
2.实现文本文件的复制(通过FileReader和FileWriter)
 对于非文本文件(图片、视频)  只能使用字节流
public void testFileReaderWriter() {
		FileReader fr = null;
		FileWriter fw = null;
		try {
			// 原始文件
			File src = new File("oop.txt");
			// 目标文件
			File dest = new File("oop");
			// 字符输入流对象
			fr = new FileReader(src);
			// 字符输出流对象
			fw = new FileWriter(dest);
			// 用来存放读取的数据
			char[] c= new char[24];
			int len;
			while ((len = fr.read(c)) != -1) {
				// 写入数据
				fw.write(c, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
3.把一个图片的内容写入到新的文件
public void testBufferedInputOutputStream() {
		// 字节输入缓冲流
		BufferedInputStream bis = null;
		// 字节输出缓冲流
		BufferedOutputStream bos = null;
		
		try {
			// 1.提供读写的文件
			File file1 = new File("one.jpg");
			File file2 = new File("1.jpg");
			// 2.创建相应的节点流,FileInputStream,FileOutputStream
			FileInputStream fis= new FileInputStream(file1);
			FileOutputStream fos = new FileOutputStream(file2);
			// 3.将创建的节点流对象作为形参传递给缓冲流的构造器
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			// 4.实现文件复制的操作
			byte[] b = new byte[1024];
			int len;
			// 读数据
			while ((len = bis.read(b)) != -1) {
				// 写数据
				bos.write(b, 0, len);
				// 清空缓冲区数据
				bos.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
4.使用缓冲流实现文件复制
public void copyFile(String src,String dest) {
				// 字节输入缓冲流
				BufferedInputStream bis = null;
				// 字节输出缓冲流
				BufferedOutputStream bos = null;
				
				try {
					// 1.提供读写的文件
					File file1 = new File(src);
					File file2 = new File(dest);
					// 2.创建相应的节点流,FileInputStream,FileOutputStream
					FileInputStream fis= new FileInputStream(file1);
					FileOutputStream fos = new FileOutputStream(file2);
					// 3.将创建的节点流对象作为形参传递给缓冲流的构造器
					bis = new BufferedInputStream(fis);
					bos = new BufferedOutputStream(fos);
					// 4.实现文件复制的操作
					byte[] b = new byte[1024];
					int len;
					// 读数据
					while ((len = bis.read(b)) != -1) {
						// 写数据
						bos.write(b, 0, len);
						// 清空缓冲区数据
						bos.flush();
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (bos != null) {
						try {
							bos.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if (bis != null) {
						try {
							bis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值