IO流(1)

分类:按数据流分输入流(读入数据)和输出流(写入数据),按照数据类型分字节流和字符流。

  • 字节流:抽象基类 InputStream、OutputStream,子类:FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream等多个子类,其中后两者为高效流
  • 字符流:抽象基类 Reader、Writer,子类:InputStreamReader、OutputStreamWriter、FileReader、FileWriter、BufferedReader、BufferedWriter等多个,其中InputStreamReader和OutputStreamWriter为转换流,InputStreamReader(InputStream is) 可以将字节流转换成字符流,后两者为高效流。
使用字符流时,可以指定编码 InputStreamReader(InputStream is,StringcharSetName)。
flush()和close()区别:flush()刷新缓冲区,流对象还可以继续使用,close()先刷新缓冲区,关闭流对象,流对象不可以使用

具体使用如下:
  1. 基本字节流,一次读写一个字节
  2. 基本字节流,一次读写一个字节数组
  3. 高效字节流,一次读写一个字节
  4. 高效字节流,一次读写一个字节数组
  5. 字符流,一次读写一个字符
  6. 字符流,一次读写一个字符数组
  7. 高效字符流,一次读写一行

基本字节流,一次读写一个字节
	private static void method1(String src, String dest) throws Exception {
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(dest);
		int by;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}
		fis.close();
		fos.close();
	}

基本字节流,一次读写一个字节数组
	private static void method2(String src, String dest) throws Exception {
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(dest);
		byte[] bytes = new byte[1024 * 8];
		int len;
		while ((len = fis.read(bytes)) != -1) {
			fos.write(bytes, 0, len);
		}
		fis.close();
		fos.close();
	}


高效字节流,一次读写一个字节
	private static void method3(String src, String dest) throws Exception {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
		int by;
		while ((by = bis.read()) != -1) {
			bos.write(by);
		}
		bis.close();
		bos.close();

	}


高效字节流,一次读写一个字节数组
	private static void method4(String src, String dest) throws Exception {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
		byte[] bytes = new byte[1024 * 8];
		int len;
		while ((len = bis.read(bytes)) != -1) {
			bos.write(bytes, 0, len);
		}
		bis.close();
		bos.close();
	}


字符流,一次读写一个字符
	private static void method5(String src, String dest) throws Exception{
		
		InputStreamReader isr = new InputStreamReader(new FileInputStream(src));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(dest));
		int ch;
		while((ch = isr.read()) !=-1){
			osw.write(ch);
		}
		isr.close();
		osw.close();
	}


字符流,一次读写一个字符数组
	private static void method6(String src, String dest) throws Exception{
		
		InputStreamReader isr = new InputStreamReader(new FileInputStream(src));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(dest));
		char[] chs = new char[8*1024];
		int len;
		while((len = isr.read(chs)) !=-1){
			osw.write(chs,0,len);
		}
		isr.close();
		osw.close();
	}


高效字符流,一次读写一行
	private static void method7(String src, String dest) throws Exception{
		BufferedReader br = new BufferedReader(new FileReader(src));
		BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
		String line;
		while((line = br.readLine()) != null){
			bw.write(line);
		}
		br.close();
		bw.close();
	}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值