zoukankan      html  css  js  c++  java
  • 字节输入输出流的代码注意事项

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

    字节输入流:

    [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. }  
  • 相关阅读:
    URAL 2067 Friends and Berries (推理,数学)
    URAL 2070 Interesting Numbers (找规律)
    URAL 2073 Log Files (模拟)
    URAL 2069 Hard Rock (最短路)
    URAL 2068 Game of Nuts (博弈)
    URAL 2066 Simple Expression (水题,暴力)
    URAL 2065 Different Sums (找规律)
    UVa 1640 The Counting Problem (数学,区间计数)
    UVa 1630 Folding (区间DP)
    UVa 1629 Cake slicing (记忆化搜索)
  • 原文地址:https://www.cnblogs.com/xyou/p/7146107.html
Copyright © 2011-2022 走看看