基于字节的IO操作

包括:

  1. FileInputStream
  2. ByteArrayInputStream
  3. ObjectInputStream
  4. BufferedInputStream
  5. DataInputStream

上面 5 个为常用的基于字节操作的 IO 操作类,其中前三个 相当于 装饰器模式的 ConcreteComponent,后两个是具体的装饰者类。传送门:装饰器模式


一. FileInputStream
    用于读取文件内容。常用的有两种构造函数,例如:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public FileInputStream(String name) throws FileNotFoundException {  
  2.     this(name != null ? new File(name) : null);  
  3. }  
  4.   
  5. public FileInputStream(File file) throws FileNotFoundException {  
  6.     ...  
  7. }  
    其中 name 和 file 都是指向 需要读取的文件。Demo:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Test{  
  2.     public static void main(String[] args) {  
  3.         InputStream inputStream = null;  
  4.     try {  
  5.         inputStream = new FileInputStream("/Users/yunxin/Desktop/a");  
  6.         byte data[] = new byte[inputStream.available()];  
  7.         inputStream.read(data);  
  8.         System.out.println(new String(data));  
  9.               
  10.     } catch (FileNotFoundException e) {  
  11.         e.printStackTrace();  
  12.     } catch (IOException e) {  
  13.         e.printStackTrace();  
  14.     } finally {  
  15.         if(inputStream != null){  
  16.             try {  
  17.             inputStream.close();  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         }  
  22.     }   
  23.     }  
  24. }  
    需要注意的是 try-catch-finally

二. ByteArrayInputStream
    ByteArrayInputStream,主要是应对流的来源和目的地不一定是文件这种情况,比如说可能是内存,可能是数组。例如:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Test{  
  2.     public static void main(String[] args) {  
  3.     byte data[] = "abc".getBytes();  
  4.     InputStream inputStream = new ByteArrayInputStream(data);  
  5.           
  6.     byte data0[] = new byte[data.length];   
  7.     try {  
  8.         inputStream.read(data0);  
  9.         System.out.println(new String(data0));  
  10.     } catch (IOException e) {  
  11.         e.printStackTrace();  
  12.     } finally {  
  13.         if (inputStream != null) {  
  14.         try {  
  15.             inputStream.close();  
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         }  
  20.     }  
  21.     }  
  22. }  

三. ObjectInputStream
     ObjectInputStream 可以用于读取对象,但是读取的对象必须实现 Serializable 接口
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Test{  
  2.     public static void main(String[] args) {  
  3.     ObjectOutputStream objectOutputStream = null;  
  4.     try {  
  5.         objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));  
  6.         User user = new User();  
  7.         user.setNum(2);  
  8.         objectOutputStream.writeObject(user);  
  9.     } catch (FileNotFoundException e) {  
  10.         e.printStackTrace();  
  11.     } catch (IOException e) {  
  12.         e.printStackTrace();  
  13.     } finally {  
  14.         if(objectOutputStream != null){  
  15.             try {  
  16.             objectOutputStream.close();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.         }  
  21.     }  
  22.     }  
  23. }  
  24. class User implements Serializable{    
  25.     private static final long serialVersionUID = -8987587467273881932L;    
  26.     private int num;    
  27.     public int getNum() {    
  28.         return num;    
  29.     }    
  30.     public void setNum(int num) {    
  31.         this.num = num;    
  32.     }    
  33.     @Override    
  34.     public String toString() {    
  35.         return "User [num=" + num + "]";    
  36.     }       
  37. }    

四. BufferedInputStream

    BufferedInputStream 提供了一个缓冲的功能,可以避免大量的磁盘IO。因为像FileInputStream 这种,每一次的读取,都是一次磁盘IO。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Test{  
  2.     public static void main(String[] args) {  
  3.     BufferedInputStream bufferedInputStream = null;  
  4.     try {  
  5.         bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/yunxin/Desktop/a"));  
  6.         byte data[] = new byte[bufferedInputStream.available()];  
  7.         bufferedInputStream.read(data);  
  8.         System.out.println(new String(data));  
  9.               
  10.     } catch (FileNotFoundException e) {  
  11.         e.printStackTrace();  
  12.     } catch (IOException e) {  
  13.         e.printStackTrace();  
  14.     } finally {  
  15.         if(bufferedInputStream != null){  
  16.             try {  
  17.             bufferedInputStream.close();  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         }  
  22.     }   
  23.     }  
  24. }  

五. DataInputStream
    该类的主要作用是可以返回一些基本类型或者是String类型,否则的话,只能返回byte类型的数据,利用该类,我们可以更好的操作数据。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class Test{  
  2.     public static void main(String[] args) {  
  3.         DataOutputStream dataOutputStream = null;  
  4.     try {  
  5.         dataOutputStream = new DataOutputStream(new FileOutputStream("/Users/yunxin/Desktop/a"));  
  6.         dataOutputStream.writeInt(999);  
  7.     } catch (FileNotFoundException e) {  
  8.         e.printStackTrace();  
  9.     } catch (IOException e) {  
  10.         e.printStackTrace();  
  11.     } finally {  
  12.         if(dataOutputStream != null){  
  13.             try {  
  14.             dataOutputStream.close();  
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.         }  
  19.     }   
  20.     }  
  21. }  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值