字节输入输出流的代码注意事项

输入输出是站在当前程序的角度,输入即从外界读取数据,输入即向外界输出数据。写代码时要注意以下几点,见代码:

字节输入流:

 

[java] view plain  copy
 
  1. package TestFileInputStream;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6.   
  7. public class Test {  
  8.     public static void main(String[] args) {  
  9.         String name = "E:\\day03\\123.txt"; //读取地址  
  10.         FileInputStream f = null; //new一个字节输入流  
  11.         try {  
  12.             f = new FileInputStream(name);  
  13.             int len = 0;  
  14.             StringBuffer buffer = new StringBuffer();  
  15.             byte[] b = new byte[1024];   
  16.             //现代操作系统的内存管理都具有分页机制,而内存页的大小都是1024的整数倍  
  17.             //定义1024整数倍大小的缓冲区在分配内存时将不会形成碎片。  
  18.             while(-1 != (len = f.read(b))) { //.read没东西可读时会返回-1,当返回-1时停止循环  
  19.                 String str = new String(b, 0, len);  
  20.                 //每次都将读到byte[]里的内容传给str(String类型自带数组转字符串功能)  
  21.                 buffer.append(str);//再将str拼接到StringBuffer类型变量里面  
  22.             }  
  23.             System.out.println(buffer.toString());  
  24.         } catch (FileNotFoundException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         } catch (IOException e) {  
  28.             // TODO Auto-generated catch block  
  29.             e.printStackTrace();  
  30.         } finally {  
  31.             if(f != null) {  
  32.                 try {  
  33.                     f.close();  
  34.                 } catch (IOException e) {  
  35.                     // TODO Auto-generated catch block  
  36.                     e.printStackTrace();  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41. }  


字节输出流:

 

 厦门叉车

[java] view plain  copy
 
    1. package TestFileOutputStream;  
    2.   
    3. import java.io.FileNotFoundException;  
    4. import java.io.FileOutputStream;  
    5. import java.io.IOException;  
    6.   
    7. public class Test {  
    8.     public static void main(String[] args) {  
    9.         String str = "11111asdfsad5665asd12f";  
    10.         String path = "E:\\day03\\123.txt"; //输出路径要是绝对路径,即输出到哪个文件,不能只是目录  
    11.         FileOutputStream f = null; //字节输出流  
    12.         try {  
    13.             f = new FileOutputStream(path, true);  
    14.             byte[] bytes = str.getBytes(); //把要输出的字符串变成字节数组  
    15.             f.write(bytes); //字节流写入只能传字节  
    16.             f.flush();  
    17.         } catch (FileNotFoundException e) {  
    18.             // TODO Auto-generated catch block  
    19.             e.printStackTrace();  
    20.         } catch (IOException e) {  
    21.             // TODO Auto-generated catch block  
    22.             e.printStackTrace();  
    23.         } finally {  
    24.             if(f != null) { //判断f是否为空,若为空就说明没动过,不用关闭  
    25.                 try {  
    26.                     f.close();  
    27.                 } catch (IOException e) {  
    28.                     // TODO Auto-generated catch block  
    29.                     e.printStackTrace();  
    30.                 }  
    31.             }  
    32.         }  
    33.           
    34.     }  
    35. }  

转载于:https://www.cnblogs.com/xyou/p/7146107.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值