黑马程序员--IO流--字节流

----------------- android培训java培训、期待与您交流!-----------------


在上一篇中日志已经讲述了字符流及字符缓冲区的基本使用,接下来这篇主要是讲述IO流中的另一类,字节流InputStream,OutputStream的使用字节流的使用范围广,可通用如文本,音频,视频文件而字符流只能用于文本数据读写.所以一般情况下更多的是使用字节流的。下面来看看字节流的使用方式。

public class StreamDemo2 {

	public static void main(String[] args) {
		File  f1 =new File("fileOutStream.txt");
		writeOutStream(f1);
		readOutStream(f1);
	}
	
	/**
	 * 下面就看看字节流写入数据的操作,这与字符流的操作是相类似的
	 * 写入步骤
	 * 1   创建字节流对象,指定操作的文件
	 * 2   操作字节流对象
	 * 3   关闭流
	 * @param  filename  存放写入内容的文件
	 */
	public static void  writeOutStream(File filename){
		FileOutputStream  fos =null;	//创建FileOutputStream字节流对象指向null
		try {		
			//实例化对象,指定操作的文件
			fos =new FileOutputStream(filename);
			//字节流写入接受的是字节参数
			fos.write("字节流写入操作实例--内容".getBytes());
			//flush()--刷新流对象缓冲区中的数据
			//将数据刷新到文件中,要注意每次写入文件是都要刷新下
			fos.flush();	
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(fos!=null)	//判断对象是否存在,存在则关闭流
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	

	/**
	 * 字节流FileOutputStream读取数据
	 * 读取数据步骤
	 * 1  创建读取字节流对象,指定操作文件
	 * 2  操作字节流
	 * 3  关闭流对象
	 * @param filesrc   要读取的文件
	 */
	public static void readOutStream(File srcFile){
		FileInputStream fis =null;
		try {
			fis =new FileInputStream(srcFile);
			/**
			 * 使用字节流读取文件有2种方法
			 * 第一种  
			 * 创建字节数组,用来接收数据
			 */
			byte [] by =new byte[1024];
			int num;
			while ((num=fis.read(by))!=-1) {
				System.out.println(new String(by,0,num));
			}
			/**
			 * 第二种方式
			 * available()--获取的是文件的字节大小
			 * 使用available的好处---刚好定义了字节数组的大小,不用循环判断来读取
			 *        缺点---如果文件过大,那么定义的字节数组会超过上限-崩溃
			 *一般还是建议使用第一种,小文件可使用第二种
			 */
			byte [] by2 =new byte[fis.available()];
			fis.read(by2);
			System.out.println(new String(by2));			
			//
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(fis!=null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
在字符流中已经知道了字符流中加入了缓冲机制,那么同样的在字节流中也加入了缓冲区下面就来看看字节流更高效的使用方式,通过一个文件复制的功能来演示下。具体代码如下:

public class StreamDemo2 {

	public static void main(String[] args) {
		File  f1 =new File("愿得一人心.mp3");
		File  f2 =new File("愿得一人心_copy.mp3");
		copyBuffStream(f2, f3);
	}

	/**
	 * 下面这就是一个加入缓冲的应用,使用字节流复制已有的mp3文件
	 * 步骤
	 * 1  分别创建读取写入的缓冲对象,指定操作的文件
	 * 2 进行读写操作,在缓冲区中每读一次,就写一次
	 * 3 关闭缓冲对象
	 * @param srcFile   源文件
	 * @param copyFile   复制的文件
	 */
	public static void copyBuffStream(File srcFile,File copyFile){
		BufferedInputStream  bis = null;   //分别创建缓冲字节流对象
		BufferedOutputStream bos =null;
		try {
			bis =new BufferedInputStream(new FileInputStream(srcFile));  //实例化,指定操作文件
			bos=new BufferedOutputStream(new FileOutputStream(copyFile));
			byte [] by = new byte[1024];  //保存字节的数组
			int num;
			while((num =bis.read(by))!=-1){
				bos.write(by, 0, num);
				bos.flush();   //写入后刷新处理
			}			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//分别判断读写的缓冲区对象是否存在,存在则关闭缓冲区
			if(bis!=null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if(bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
字节流的处理看的差不多了,接下来就是转换流的概念:

转换流 ----    InputStreamReader     OutputStreamWriter

转换流的出现实现了指定编码的操作,和优化了数据的读取和写入。

1  当我们要使用非默认的编码时,我们需要使用转换流提供的因为字节流的重载构造方法中有指定编码格式的参数。
     当我们使用默认编码时下面的代码效果是一样的
     BufferedWriter  bw =new BufferedWriter(new FileWriter("aa.txt"));
     BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xxx.txt")));
     如果我们要保存为UTF-8或其他的的编码时就只能这样写
     BufferedWriter  bw  =new BufferedWriter(new OutPutStreamWriter(new FileOutputStream("xx.txt"),"UTF-8"));
     而一旦使用转换流指定了编码那么当读取该文件时就就要以指定的编码读取文件才能正确的读取否则会出现乱码问题
     BufferedReader  br  =new BufferedReader(new InputStreamReader(new FileInputStream("xx.txt"),"UTF-8"));
2  字符与字节数据间的转换操作--便于提高效率
    因为在BufferedReader中有特殊的读取方式readLine()--整行读取。如果字节流想要是用就得转换--使用InputStreamReader以               及在BufferedWriter中的NewLine()换行方法和缓冲
    因为计算机处理的都是字节数据所以
读取时----将字符转换为字节BufferedReader,FileReader
 输出时----将字节转换为字符BufferedWriter,FileWriter
  不管操作字符还是字节数据,如果提高效率那么一般是使用转换流如果使用指定编码那么一定使用转换流




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值