学习笔记之JavaSE(40)--IO流2

今天学习的内容是字节流和字节流缓冲区


字节流与字符流的基本操作相同,注意字节流的输出没有用到缓冲区,下例使用FileOutputStream类FileInputStream类实现对文本文件的读写(字节流不能直接处理字符内容,只能直接处理字节内容):

public class Test78 {

	public static void main(String[] args) {

		test_write();
		test_read_1();
		test_read_2();
	}

	private static void test_read_2() {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("test.txt");
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				System.out.print(new String(buf, 0, len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}

	private static void test_read_1() {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("test.txt");
			int ch = 0;
			while ((ch = fis.read()) != -1) {
				System.out.print((char) ch);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}

	private static void test_write() {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("test.txt");
			fos.write("asdasd".getBytes());// 字节流操作的是字节
			// 注意与字符流不同,字节流并没有用到缓冲区,不用flush()
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}
}

与字符流一样,字节流也有对应的缓冲区--BufferedOutputStream类和BufferedInputStream类,下面分别使用基本方法和缓冲区实现图片和MP3的复制

public class Test79 {

	public static void main(String[] args) {

		copy_picture_1();
		copy_picture_2();
		copy_mp3_1();
		copy_mp3_2();
	}

	private static void copy_mp3_2() {

		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream("萧敬腾 - Beautiful Love.mp3"));
			out = new BufferedOutputStream(new FileOutputStream("萧敬腾 - Beautiful Love_copy_2.mp3"));
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}

	private static void copy_mp3_1() {

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("萧敬腾 - Beautiful Love.mp3");
			fos = new FileOutputStream("萧敬腾 - Beautiful Love_copy_1.mp3");
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				fos.write(buf, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}

	private static void copy_picture_2() {

		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream("IO.jpg"));
			out = new BufferedOutputStream(new FileOutputStream("IO_copy_2.jpg"));
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}

	private static void copy_picture_1() {

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("IO.jpg");
			fos = new FileOutputStream("IO_copy_1.jpg");
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				fos.write(buf, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭资源失败");
				}
			}
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值