黑马程序员JAVA基础IO流之字节流和字节流缓冲区

  
二.字节流

  2.1 字节流基类:

  OutputStream 和 InputStream 通过这两个抽象类提供的方法可以对文件进行操作。

  OutputStream 类中包含的常用方法:

  > void close() : 关闭此输出流并释放与此流有关的所有系统资源。

  > void flush() :刷新此输出流并强制写出所有缓冲的输出字节。

  > void writer(byte[] b ) : 将 b.length 个字节从指定的 byte 数组写入此输出流。

  > void writer(byte[] b , int off , int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。

  > abstract void writer(int b )  将指定的字节写入此输出流。

  注意: 和Writer 类中的writer 方法不同的是,Writer 类中的writer 操作的是字符串,而OutputStream 中操作的是 字节。

  InputStream 类中常用的方法:

  > void close() : 关闭流。

  > abstract int read() 从输入流中读取数据的下一个字节。 

  > int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
  > int read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入 byte 数组。

  > int available() : 方法返回的是数据的长度,包括换行符和回车符。

  OutputStream 和 InputStream类的基本演示:

 1 public class FileOutputStreamDemo {
 2     public static void main(String[] arg) throws IOException {
 3 //        写文件。
 4         OutputStream fos = new FileOutputStream("D:\\OutputStream.txt") ;
 5         
 6         fos.write("jfwhjef".getBytes()) ; 
 7         fos.close() ;
 8 //        读文件。
 9         InputStream is = new FileInputStream("D:\\OutputStream.txt") ;
10 //        方法一:
11 //        int  ch = 0 ; 
12 //        while((ch = is.read()) != -1) {
13 //            System.out.println((char)ch);
14 //        }
15 //        is.close() ;
16 //        方法二:推荐使用这个。
17 //        int ch = 0 ;
18 //        byte[] bt = new byte[1024] ; 
19 //        while((ch = is.read(bt) )!= -1) {
20 //            System.out.println(new String(bt,0,ch));
21 //        }
22 //        is.close() ;
23 //        方法三:如果数据较大不推荐使用
24         byte[] bt = new byte[is.available()] ; // 定义一个刚刚好的缓冲区,不用在循环了
25         is.read(bt) ;
26         System.out.println(new String(bt));
27     }
28 }

  对于上诉的三种读取方式,推荐使用第二种。第一种因频繁的读取刷新效率低;而第三种,因为虚拟机运行时默认开的内存是64M, 如果要操作的数据是1G多,则不可能在内存中开出这么大的空间。 

  练习:拷贝一个图片。

  当需要操作图片、音频等数据,这时就要用到字节流。

  思路:
  1、用字节读取流对象和图片关联。
  2、用字节写入流对象创建一个图片文件,用于存储获取到的图片数据。
  3、通过循环读写,完成数据的存储。
  4、关闭资源。

 1 public class CopyPic {
 2     public static void main(String[] args) {
 3         FileInputStream fis = null ; 
 4         FileOutputStream fos = null ; 
 5         try {
 6             fis = new FileInputStream("C:\\Users\\Administrator\\Downloads\\1.jpg") ;
 7             fos = new FileOutputStream("D:\\1.jpg") ; 
 8 //            方法一:
 9 //            int leng = 0  ;
10 //            byte[] bt = new byte[1024] ; 
11 //            while ((leng = fis.read(bt)) != -1) {
12 //                fos.write(bt,0,leng) ; 
13 //            }
14 //            方法二;
15             byte[] bt = new byte[fis.available()];
16             fis.read(bt) ; 
17             fos.write(bt);
18         }catch(Exception e) { 
19         }finally{
20             try{
21                 if(fos != null)
22                     fos.close() ; 
23             }catch(Exception e){ 
24             } 
25             try {
26                 if (fis != null) 
27                     fis.close() ; 
28             }catch(Exception e) { 
29             }
30         }
31     }
32 }

  

  2.2 字节流缓冲区

  字节流缓冲区对应的类:BufferedOutputStream 和BufferedInputStream 。

  练习:通过缓冲区,完成Mp3的复制。 

 1 public class CopyMp3 {
 2     public static void main(String[] args) throws IOException { 
 3         Copy_1() ;  
 4     }
 5 //    通过字节流的缓冲区完成复制。
 6     public static void Copy_1() throws IOException {
 7         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
 8                 "F:\\KuGou\\Suede - Beautiful Ones.mp3")) ;
 9         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
10                 "D:\\1.mp3")) ;
11         int by = 0  ; 
12         while((by = bis.read()) != -1) {
13             bos.write(by) ; 
14         }
15         bos.close(); 
16         bis.close() ;
17     }
18 }
原文地址:https://www.cnblogs.com/jbelial/p/3052719.html