• Java学习 之 IO流(字节流、字符流)


      1 /*
      2 
      3 字节输出流: OutputStream
      4 
      5 OutputStream是抽象类,表示输出字节流的所有类的超类,操作的数据都是字节
      6 
      7     void  close()关闭此输出流并释放与此流有关的所有系统资源
      8 
      9     void flush()刷新此输出流并强制写出所有缓冲的输出字节
     10 
     11     void write(byte[] b) 将b.length个字节从指定的byte数组写入此输出流
     12 
     13     void write(byte[] b , int off , int len)将指定byte数组从偏移量off开始的len个字节写入此输出流
     14 
     15     abstract void write(int b)将指定的字节写入此输出流
     16 
     17 FileOutputStream类即文件输出流,用于将数据写入File的输出流
     18 
     19     FileOutputStream(Fille file)创建一个指定File对象表示的文件中写入数据的文件输出流
     20 
     21     FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的输出文件流
     22 
     23 */
     24 
     25 public class FileOutPutStreamDemo{
     26 
     27    public static void main(String[] args){
     28 
     29        //创建存储数据的文件
     30 
     31        File file = new File("c:\a.txt");
     32 
     33        //创建一个用于操作文件的字节输出流对象,一创建就必须明确数据存储的目的地
     34 
     35       //输出流目的是文件,会自动创建。如果文件存在,则覆盖
     36 
     37       FileOutputStream fos = new FileOutputStream(file);
     38 
     39      //调用父类的write方法
     40 
     41      byte[] b = "abcde".getBytes();
     42 
     43      fos.write(b);
     44 
     45      //关闭流资源
     46 
     47     fos.close();
     48 
     49    }
     50 
     51 }
     52 
     53 /*
     54 
     55 文件续写和换行
     56 
     57 FileOutputStream(File file , boolean append)创建一个向指定File对象表示的文件中写入数据的文件输出流
     58 
     59 FileOutputStream(String name , boolean append)创建一个向指定name的文件中写入数据的文件输出流
     60 
     61 */
     62 
     63 public class FileOutputStreamDemo2{
     64 
     65     public static void main(String[] args){
     66 
     67         File file = new File("c:\a.txt");
     68 
     69         FileOutputStream fos = new FileOutputStream(file , true);
     70 
     71         String str = "
    " +"ab";
     72 
     73         fos.write(str.getBytes());
     74 
     75         fos.close();
     76 
     77    }
     78 
     79 }
     80 
     81 /*
     82 
     83 IO异常的处理
     84 
     85 */
     86 
     87 public class FileOutputStreamDemo3{
     88 
     89     public static void main(String[] args){
     90 
     91      File file = new File("c:\a.txt");
     92 
     93     //定义FileOutputStream的引用
     94 
     95    FileOutputStream fos = null;
     96 
     97    try{
     98 
     99       //创建FileOutputStream对象
    100 
    101      fos = new FileOutputStream(file);
    102 
    103       //写出数据
    104 
    105      fos.write("abc".getBytes());
    106 
    107        }catch(IOException e){
    108 
    109          System.out.println(e.toString() + "------");
    110 
    111        }finally{
    112 
    113          //一定要判断fos是否为null,只有不为null时,才可以关闭资源
    114 
    115         if(fos != null){
    116 
    117             try{
    118 
    119              fos.close();
    120 
    121                 }catch(IOException e){
    122 
    123                 throw new RuntimeException("");
    124 
    125                 }
    126 
    127              }
    128 
    129        }
    130 
    131     }
    132 
    133 }
    134 
    135 /*
    136 
    137 字节输入流:InputStream
    138 
    139 InputStream是抽象类,表示字节输入流的所有类的超类
    140 
    141   abstract int read()从输入流中读取数据的下一个字节
    142 
    143   int read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
    144 
    145 FileInputStream类即文件输入流
    146 
    147  abstract int read()从输入流中读取数据的下一个字节
    148 
    149 */
    150 
    151 public class FileInputStreamDemo{
    152 
    153     public static void main(String[] args){
    154 
    155      File file = new File("c:\a.txt");
    156 
    157     //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取和数据源相关联
    158 
    159    FileInputStream fis = new FileInputStream(file):
    160 
    161   //读取数据,一次读一个字节
    162 
    163   int ch = 0;
    164 
    165   while((ch = fis.read()) != -1){
    166 
    167     System.out.println( "ch=" + (char)ch);
    168 
    169   }
    170 
    171   //关闭资源
    172 
    173    fis.close();
    174 
    175     }
    176 
    177 }
    178 
    179 /*
    180 
    181 读取数据read(byte[] b)
    182 
    183 */
    184 
    185 public class FileInputStreamDemo2{
    186 
    187     public static void main(String[] args){
    188 
    189     File file = new File("c:\a.txt"); 
    190 
    191     //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取和数据源相关联
    192 
    193     FileInputStream fis = new FileInputStream(file);
    194 
    195    //创建一个字节数组
    196 
    197    byte[] b = new byte[1024];//长度可以定义成1024的整数倍
    198 
    199    int len = 0;
    200 
    201    while((len = fis.read(b)) != -1){
    202 
    203      System.out.println(new String(b , 0 , len));
    204 
    205    }
    206 
    207    fis.close();
    208 
    209    }
    210 
    211 }
    212 
    213 /*
    214 
    215 复制文件: 读取一个已有的数据, 并将这些读到的数据写入到另一个文件中
    216 
    217 */
    218 
    219 public class CopyFileTest{
    220 
    221    public static void main(String[] args){
    222 
    223    //明确源和目的
    224 
    225   File srcFile = new File("c:\a.txt");
    226 
    227   File destFile = new File("d:\a,txt");
    228 
    229   //明确字节流 输入流和源相关联,输出流和目的关联
    230 
    231   FileInputStream fis = new FileInputStream(srcFile);
    232 
    233   FileOutputStream fos = new FileOutputStream(destFile);
    234 
    235   //使用输入流的读取方法读取字节,并将字节写入到目的中
    236 
    237  int ch = 0;
    238 
    239  while((ch = fis.read()) != -1){
    240 
    241    fos.write(ch);
    242 
    243  }
    244 
    245  //关闭资源
    246 
    247  fos.close();
    248 
    249  fis.close();
    250 
    251 }
    252 
    253 }
    254 
    255 /*
    256 
    257 缓冲区方式复制文件
    258 
    259 */
    260 
    261 public class CopyFileByBufferTest{
    262 
    263    public static void main(String[] args){
    264 
    265    //明确源和目的
    266 
    267   File srcFile = new File("c:\a.txt");
    268 
    269   File destFile = new File("d:\a,txt");
    270 
    271   //明确字节流 输入流和源相关联,输出流和目的关联
    272 
    273   FileInputStream fis = new FileInputStream(srcFile);
    274 
    275   FileOutputStream fos = new FileOutputStream(destFile);
    276 
    277  //定义一个缓冲区
    278 
    279 byte[] buf = new byte[1024];
    280 
    281  int len = 0;
    282 
    283  while((len = fis.read(b)) != -1){
    284 
    285    fos.write(buf , 0 , len);//将数组中的指定长度的数据写入到输出流中
    286 
    287  }
    288 
    289  //关闭资源
    290 
    291  fos.close();
    292 
    293  fis.close();
    294 
    295 }
    296 
    297 }
    298 
    299  
    300 
    301 /*
    302 
    303 字符流:
    304 
    305           字符输入流: Reader,读取字符流的抽象超类
    306 
    307                               int read()读取单个字符
    308 
    309                               int read(char[] cbuf)将字符读入数组
    310 
    311                               FileReader类:
    312 
    313                               FileReader(File file)在给定从中读取数据的File的情况下创建一个新FileReader
    314 
    315                               FileReader(String fileName)在给定从中读取数据的文件名的情况下创建一个新FileReader
    316 
    317 */
    318 
    319 //字节流读取字符的问题
    320 
    321 public class CharStreamDemo{
    322 
    323     public static void main(String[] args) throws IOException{
    324 
    325         //读取文件中的中文
    326 
    327        readCNTest();
    328 
    329        //给文件中写中文
    330 
    331       writeCNTest();
    332 
    333     }
    334 
    335    //读取中文
    336 
    337   public static void readCNTest() throws IOException{
    338 
    339      FileInputStream fis = new FileInputStream("c:\a.txt");
    340 
    341      int ch = 0;
    342 
    343     while((ch = fis.read) != -1){
    344 
    345        System.out.println(ch);
    346 
    347     }
    348 
    349   }
    350 
    351   //写中文
    352 
    353   public static void writeCNTest() throws IOException{
    354 
    355    FileOutputStream fos = new FileOutputStream("c:\a.txt");
    356 
    357     fos.write("你好".getBytes());
    358 
    359      fos.close();
    360 
    361   }
    362 
    363 }
    364 
    365 //FileReader读取包含中的文件
    366 
    367 public class CharStreamDemo2{
    368 
    369     public static void main(String[] args) throws IOException{
    370 
    371         //读取文件中的中文
    372 
    373        readCNTest();
    374 
    375        //给文件中写中文
    376 
    377       writeCNTest();
    378 
    379     }
    380 
    381    //读取中文
    382 
    383   public static void readCNTest() throws IOException{
    384 
    385      FileInputStream fis = new FileInputStream("c:\a.txt");
    386 
    387      int ch = 0;
    388 
    389     while((ch = fis.read) != -1){
    390 
    391        //输出的字符对应的编码值
    392 
    393        System.out.println(ch);
    394 
    395        //输出字符本身
    396 
    397       System.out.println((char)ch);
    398 
    399     }
    400 
    401   }
    402 
    403   //写中文
    404 
    405   public static void writeCNTest() throws IOException{
    406 
    407    FileOutputStream fos = new FileOutputStream("c:\a.txt");
    408 
    409     fos.write("你好".getBytes());
    410 
    411      fos.close();
    412 
    413   }
    414 
    415 }
    416 
    417 /*
    418 
    419 字符输出流Writer:写入字符流的抽象超类
    420 
    421                            void write(char[] cbuf)写入字符数组
    422 
    423                            abstract void write(char[] cbuf , int off , int len)写入字符数组的某一部分
    424 
    425                            void write(int c)写入单个字符
    426 
    427                            void write(String str)写入字符串
    428 
    429                            void write(String str , int off , int len)写入字符串的某一部分
    430 
    431 FileWriter类:
    432 
    433                     FileWriter(File file)根据给定的File对象构造一个FileWriter对象
    434 
    435                     FileWriter(File file , boolean append)根据给定的File对象构造一个FileWriter对象
    436 
    437                     FileWriter(String fileName)根据给定的文件名构造一个FileWriter对象
    438 
    439                     FileWriter(String fileName , boolean append)根据给定的文件名及指示是否附加写入数据的boolean值来构造FileWriter对象
    440 
    441 */
    442 
    443 public class FileWriterDemo{
    444 
    445     public static void main(String[] args){
    446 
    447     FileWriter fw = new FileWriter("c:\a.txt");
    448 
    449     fw.write("你好");
    450 
    451     fw.flush();
    452 
    453    fw.close();  
    454 
    455   }
    456 
    457 }
    458 
    459 /*
    460 
    461 flush()和close()区别:
    462 
    463   abstract void close()关闭流 ,  但要先刷新
    464 
    465   abstract void flush()刷新该流的缓冲
    466 
    467  flush():将流中的缓冲区的数据刷新到目的地中,刷新后, 流还可以继续使用
    468 
    469  close():关闭资源,但在关闭前会将缓冲区中的数据先刷新到目的地,否则丢失数据,然后在关闭流,
    470 
    471              流不可以使用。如果写入数据多,一定要一边写一边刷新,最后一次可以不刷新,由close完成刷新并关闭
    472 
    473 */
  • 相关阅读:
    【六道无鱼】ExifTool编辑修改图片GPS
    【Elastic】Filebeat+ELK日志收集分析方案
    【Cesium】鹰眼地图功能
    【Cesium】3dtiles模型单体化
    【PHP】Version 7.2.13 报错 Fatal error: Call to undefined function curl_init()解决方案
    【ODM】win10 安装 webODM
    【数据库】mysql 删除多个关联的表
    【ElasticSearch】win10 安装elasticSearch 6.6.1
    【Cesium】视域分析 基于3dtiles做的视域分析
    缕清思路,继续前行
  • 原文地址:https://www.cnblogs.com/z97-/p/12744180.html
Copyright © 2020-2023  润新知