zoukankan      html  css  js  c++  java
  • Java学习 之 IO流(转换流、缓冲流)

      1 /*
      2 
      3 OutputStreamWriter:将字符串按照指定的编码表转换成字节,在使用字节流将字节写出去
      4 
      5 */
      6 
      7 public class OutputStreamWriterDemo{
      8 
      9     public static void main(String[] args) throws IOException{
     10 
     11         //字符转字节的转换流
     12 
     13         writeCN();
     14 
     15    }
     16 
     17    public static void writeCN() throws IOException{
     18 
     19        //创建与文件关联的字节输出流对象
     20 
     21        FileOutputStream fos = new FileOutputStream("c:\a.txt");
     22 
     23        //创建可以把字符转换成字节的转换流对象,并指定编码
     24 
     25        OutputStreamWriter osw = new OutputStreamWriter(fos , "utf-8");
     26 
     27        //调用转换流,把文字写出去,其实是写到转换流的缓冲区
     28 
     29       osw.write("你好");//写入缓冲区
     30 
     31       osw.close();
     32 
     33    }
     34 
     35 }
     36 
     37 /*
     38 
     39 InputStreamReader:是字节流通向字符流的桥梁,使用指定的字符编码表读取字节并将其解码为字符
     40 
     41 */
     42 
     43 public class InputStreamReaderDemo{
     44 
     45      public static void main(String[] args) throws IOException{
     46 
     47        //字节转换字符流的转换流
     48 
     49        readCN();
     50 
     51     }
     52 
     53     public static void readCN() throws IOException{
     54 
     55           //创建读取文件的字节流对象
     56 
     57          FileInputStream fis = new FileInputStream("c:\a.txt");
     58 
     59          //创建转换流对象
     60 
     61         InputStreamReader isr = new InputStreamReader(fis , "utf-8");
     62 
     63         //使用转换流去读字节流中的字节
     64 
     65        int ch = 0;
     66 
     67       while((ch = isr.read()) != -1){
     68 
     69            System.out.println((char)ch);
     70 
     71       }
     72 
     73       isr.close();
     74 
     75     }
     76 
     77 }
     78 
     79 /*
     80 
     81 缓冲流:
     82 
     83            字节缓冲输出流:BufferedOutputStream
     84 
     85             构造方法:public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流
     86 
     87 */
     88 
     89 public class BufferedOutputStreamDemo{
     90 
     91     public static void main(String[] args) throws IOException{
     92 
     93         //写数据到文件的方法
     94 
     95        write();
     96 
     97     }
     98 
     99     /*
    100 
    101     写数据到文件的方法
    102 
    103     1、创建流
    104 
    105     2、写数据
    106 
    107     3、关闭流
    108 
    109    */
    110 
    111     public static void write() throws IOException{
    112 
    113       //创建基本的字节输出流
    114 
    115      FileOutputStream fos = new FileOutputStream("a.txt");
    116 
    117     //使用高效的流,把基本的流进行封装,实现速度的提升
    118     BufferedOutputStream bos = new BufferedOutputStream(fos);
    119 
    120     //写数据
    121 
    122     out.write("你好".getBytes());
    123 
    124     //关闭流
    125 
    126    out.close();
    127 
    128    }
    129 
    130 }
    131 
    132 /*
    133 
    134 字节缓冲输入流:BufferedInputStream
    135 
    136 构造方法:public BufferedInputStream(InputStream in)
    137 
    138 */
    139 
    140 public class BufferedInputStream{
    141 
    142      public static void main(String[] args) throws IOException{
    143 
    144          read();
    145 
    146     }
    147 
    148  /*
    149 
    150          从文件中读取数据
    151 
    152         1、创建缓冲流对象
    153 
    154         2、读数据,打印
    155 
    156         3、关闭
    157 
    158       */
    159 
    160    public static void read() throws IOException{
    161 
    162       //创建缓冲流对象
    163 
    164       FileInputStream fis = new FileInputStream("a.txt");
    165 
    166      //把基本的流包装成高效的流
    167 
    168      BufferedInputStream bis = new BufferedInputStream(fis);
    169 
    170      //读数据
    171 
    172     int ch = -1;
    173 
    174    while((ch = bis.read()) != -1){
    175 
    176       System.out.println((char)ch);
    177 
    178     }
    179 
    180     bis.close();
    181 
    182    }
    183 
    184 }
    185 
    186 /*
    187 
    188 字符缓冲输入流:BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
    189 
    190 public String readLine()读取一个文本行,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回true
    191 
    192 字符缓冲输出流:BufferedWriter将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入
    193 
    194 void newLine()根据当前的系统,写入一个换行符
    195 
    196 */
    197 
    198 public class BufferedWriterDemo{
    199 
    200      public static void main(String[] args) throws IOException{
    201 
    202        //创建流
    203 
    204       //基本字符输出流
    205 
    206      FileWriter fw = new FileWriter("file.txt");
    207 
    208      //把基本的流进行包装
    209 
    210      BufferedWriter bw = new BufferedWriter(fw);
    211 
    212     //写数据
    213 
    214     for(int i = 0 ; i < 5 ; i++){
    215 
    216          bw.write("hello");
    217 
    218          bw.newLine();
    219 
    220    }
    221 
    222    //关闭流
    223 
    224   bw.close(); 
    225 
    226     }
    227 
    228 }
    229 
    230 public class BufferedWriterDemo{
    231 
    232      public static void main(String[] args) throws Exception{
    233 
    234         //创建流
    235 
    236        BufferedReader br = new BufferedReader("file.txt");
    237 
    238        //读数据
    239 
    240       //一次一个字符
    241 
    242      //一次一个字符数组
    243 
    244     //一次读取文本中一行的字符串内容
    245 
    246    String line = null;
    247 
    248    while((line = br.readLine()) != -1){
    249 
    250      System.out.println(line);
    251 
    252     }
    253 
    254    //关闭流
    255 
    256    line.close();
    257 
    258     }
    259 
    260 }
  • 相关阅读:
    HDU3068 最长回文
    本周最后一天——4.18
    一周又结束了——4.14
    一本通1591:数字计数
    一本通1589:不要 62
    一本通1588:数字游戏
    洛谷P2657 [SCOI2009] windy 数
    一本通1587: 【例 3】Windy 数
    一本通1586:【 例 2】数字游戏
    安卓自动化测试(一)
  • 原文地址:https://www.cnblogs.com/z97-/p/12756031.html
Copyright © 2011-2022 走看看