zoukankan      html  css  js  c++  java
  • IO流之字节流

    字节输出流OutputStream

    OutputStream为抽象类,所以要使用他的子类FileOutputStream。

    构造方法

     在创建对象是,它需要接受一个File对象或字符串,来指定位置。

    单个字节/单个字节数组的输出:

     1         //明确数据源
     2         FileOutputStream fos = new FileOutputStream("D:\\io0429\\demo01.txt");
     3         //写入
     4         fos.write(100);//d
     5         byte[] bytes = {-49,-48,-48,-66};
     6         fos.write(bytes);//闲芯
     7         fos.write(bytes,1,2);//
     8         fos.write("你好\r\n".getBytes());//你好
     9         fos.write(65);//A
    10         
    11         fos.close();

    这里要注意几点,第一,当文件地址不存在时,会直接创建文件,然后写入。第二,因为是单个字节的输出,所以当写入的值在0-127走ASCII表,如果是负数,走GBK码表,一个汉字两个字节,使用结束后记得释放资源。

    字节输入流InputStream

    构造方法与输出流相同。

    单个字节输入

    1         //明确数据源
    2         FileInputStream fis = new FileInputStream("D://io0429//demo01.txt");
    3         int len = 0;
    4         while((len = fis.read()) != -1){
    5             System.out.println((char)len);
    6         }
    7         //释放资源
    8         fis.close();

    在读取时,单个单个字节读取使用read()方法,这个方法有个返回时,它在读不到数据时,会返回-1值,所以-1可以作为跳出循环的判定条件,就得到了此循环。

    单个字节数组输入

     1         //明确数据源
     2         FileInputStream fis = new FileInputStream("D://io0429//demo01.txt");
     3         byte[] bytes = new byte[2];
     4         //一个字节数组一个字节数组读
     5         int len = 0;
     6         while((len = fis.read(bytes)) != -1){
     7             System.out.println(new String(bytes,0,len));
     8         }
     9         //释放资源
    10         fis.close();

    先创建一个byte数组,规定的范围就是一次读取的个数,判定条件一样,当读不到值时,返回-1,但有个情况,就是可能规定两个,但只有一个新值,所以我们可以使用String中的构造方法,来规定输入个数

     字节流练习:复制文件

    单字节复制

     1         //单个字节复制
     2         //明确数据源
     3         FileInputStream fis = new FileInputStream("D://io0429//demo01.txt");
     4         //明确目的地
     5         FileOutputStream fos = new FileOutputStream("D://io0429//hello//demo01.txt");
     6         int len = 0;
     7         while((len = fis.read()) != -1){
     8             //写一个字节
     9             fos.write(len);
    10         }
    11         //释放资源
    12         fos.close();
    13         fis.close();

    单字节数组复制

     1         //字节数组复制
     2         //明确数据源
     3         FileInputStream fis = new FileInputStream("D://io0429//demo01.txt");
     4         //明确目的地
     5         FileOutputStream fos = new FileOutputStream("D://io0429//hello/demo02.txt");
     6         byte[] bytes = new byte[1024];
     7         int len = 0;
     8         //读一个字节数组
     9         while((len = fis.read(bytes)) != -1){
    10             fos.write(bytes,0,len);
    11         }
    12         //释放资源
    13         fos.close();
    14         fis.close();
  • 相关阅读:
    17、springcloud整合lettuce使用redis
    16、springcloud整合Swagger2构建Restful服务的APIs
    15、Feign整合断路器监控Hystrix Dashboard
    14、Ribbon整合断路器监控Hystrix Dashboard
    13、如何使用断路器监控Hystrix Dashboard
    12、Feign整合断路器Hystrix
    wince中对ini文件的操作
    winform应用程序更新 带进度条
    wince隐藏任务栏
    一文学会JVM性能优化
  • 原文地址:https://www.cnblogs.com/shenhx666/p/15054864.html
Copyright © 2011-2022 走看看