黑马程序员_IO流操作2

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! ------- 

之前已经介绍了InputStream 、OutputStream的使用;Writer、Reader的使用,但是我们发现,当流操作文件太大时,一次全部写入到内存,内存可能溢出,有没有一种方法--一边读,一边缓存的方法将一个文件分几次来读或写呢,答案是有的。接下来我们看看加入缓冲功能后的操作吧。

这个程序中包含了带有缓冲功能的InputStream和OutputStream。

package Text;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Text {
	public static void main(String[] args) {
//		BufferedInput();
		BufferedOutput();
	}
	public static void BufferedInput() {
		//带有缓冲的字节输入流
		BufferedInputStream bufi = null;//为什么如此去写呢,因为这次要使用finally对流进行关闭,如果写在内部则会报错。
		try {
			bufi = new BufferedInputStream(new FileInputStream("e:\\T1.txt"));
			StringBuilder sb = new StringBuilder();
			
			int len = -1;
			byte[] buf = new byte[1024];
			//len = -1说明文件读完了
			while((len = bufi.read(buf))!=-1) {
				sb.append(new String(buf, 0, len));
			}
			//将读取文件输出到屏幕
			System.out.println(sb);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//细致化,对文件进行判断,这样更严谨
			if(bufi!=null) {
				try {
					bufi.close();//关闭Windows的流资源
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void BufferedOutput() {
		BufferedOutputStream bufo = null;
		try {
			//带有缓冲的字节输出流
			bufo = new BufferedOutputStream(new FileOutputStream("e:\\T2.txt"));
			String info = "你好,这个一个带有缓冲字节流输出,对文件进行测试";
			//写操作
			bufo.write(info.getBytes(),0,info.getBytes().length);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//细致化,对文件进行判断,这样更严谨
			if(bufo != null) {
				try {
					bufo.close();//关闭Windows的流资源
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
接下来看看带有缓冲功能的Reader和Writer

package Text;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Text {
	public static void main(String[] args) {
//		BufWriter();
		BufReader();
	}
	
	public static void BufWriter() {
		BufferedWriter bufw = null;
		try {
			bufw = new BufferedWriter(new FileWriter("e:\\T2.txt"));
			String info = "测试带有缓冲的的Writer,测试文件名为T2.txt,所在路径为e盘下,来看看是否创建成功吧!";
			//写操作
			bufw.write(info);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bufw != null) {
				try {
					bufw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void BufReader() {
		BufferedReader bufr = null;
		try {
			//实例化一个带有缓冲的Reader
			bufr = new BufferedReader(new FileReader("e:\\T1.txt"));
			//下面采用readLine()方法,因此返回值不是int
			String line = null;
			StringBuilder sb = new StringBuilder();
			while((line = bufr.readLine())!=null) {
				//每读完一行,下一行会接到上一行进行存储,输出时就会连成一片
				//一次每读一行后加入换行
				sb.append(line+"\r\n");
			}
			//屏幕输出
			System.out.println(sb);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//判断
			if(bufr != null) {
				try {
					bufr.close();//关闭流操作
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
接下来要做什么呢?别着急,来一个小练习,巩固一下以上的知识点吧。

要求:利用带有缓冲功能的字节流实现图片的复制程序;利用带有缓冲功能的字符流实现文本文件的复制。

package IO字节流;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo {

	public static void main(String[] args) {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			fis = new FileInputStream("e:\\Picture.jpg");
			fos = new FileOutputStream("e:\\new.jpg");
			int len = -1;
			byte[] bytes = new byte[1024];
			
			while((len = fis.read(bytes))!=-1){
				fos.write(bytes, 0, len);
				fos.flush();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{//分开关流
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}<pre name="code" class="java"><span style="white-space:pre">			</span>//分开关流
if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}}

 

package IO字符流;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class The_1_BufferedCopy {

	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader("e:\\text1.txt"));
			bw = new BufferedWriter(new FileWriter("e:\\text1_copy.txt"));
			int len = -1;
			char[] buf = new char[1024];
			
			while((len = br.read(buf))!=-1){
				bw.write(buf, 0, len);
				bw.flush();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(br!=null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bw!=null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值