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();
						}
					}
				}
	}
发布了30 篇原创文章 · 获赞 0 · 访问量 1939

猜你喜欢

转载自blog.csdn.net/qq_38499019/article/details/104645412