IO流—字节输入流、输出流

IO流分类

如果是按照数据的流向划分:

  • 输入流
  • 输出流

如果按照处理的单位划分:

  • 字节流: 字节流读取得都是文件中二进制数据,读取到二进制数据不会经过任何的处理。
  • 字符流: 字符流读取的数据是以字符为单位的 。 字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能 识别的字符。
    字符流 = 字节流 + 解码

输入字节流体系:

  • InputStream 输入字节流的基类。 抽象
    • FileInputStream 读取文件数据的输入字节流
    • BufferedInputStream 缓冲输入字节流 缓冲输入字节流的出现主要是为了提高读取文件数据的效率。
      其实该类内部只不过是维护了一个8kb的字节数组而已。

输出字节流体系:

  • OutputStream 所有输出字节流的基类 抽象类
    • FileOutputStream 向文件 输出数据 的输出字节流
    • Bufferedoutputstream 缓冲输出字节流 BufferedOutputStream出现的目的是为了提高写数据的效率。
      内部也是维护了一个8kb的字节数组而已。

输入字节流

InputStream 所有输入字节流的基类 抽象类

  • FileInputStream 读取文件数据的输入字节流

FileInputStream

使用FileInputStream读取文件数据的步骤:

  1. 找到目标文件
  2. 建立数据的输入通道
  3. 读取文件中的数据
  4. 关闭资源

方法一:缺陷: 无法读取完整一个文件 的数据.


public static void readTest1() throws IOException{
    File file = new File("F:\\a.txt");//1. 找到目标文件
    FileInputStream fileInputStream = new FileInputStream(file);//建立数据的输入通道。
    int content = fileInputStream.read(); // read() 读取一个字节的数据,把读取的数据返回。
    System.out.println("读到的内容是:"+ (char)content);
     fileInputStream.close();//关闭资源    实际上就是释放资源。 
}

方法二:使用循环读取文件的数据

public static void readTest2() throws IOException{
        File file = new File("F:\\美女\\1.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        int content = 0; //声明该变量用于存储读取到的数据
        while((content = fileInputStream.read())!=-1){
            System.out.print((char)content);
        }
        fileInputStream.close();

        long endTime = System.currentTimeMillis();
        System.out.println("读取的时间是:"+ (endTime-startTime)); //446
    }

方法三:使用缓冲 数组 读取。 缺点: 无法读取完整一个文件的数据。

public static void readTest3() throws IOException{
    File file = new File("F:\\a.txt");
    FileInputStream fileInputStream = new FileInputStream(file);
    //建立缓冲字节数组,读取文件的数据。
    byte[] buf = new byte[1024];  
    int length = fileInputStream.read(buf); // 如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。
    System.out.println("length:"+ length);

    //使用字节数组构建字符串
    String content = new String(buf,0,length);
    System.out.println("内容:"+ content);

    fileInputStream.close();
}

方法四:

public static void readTest4() throws IOException{
    File file = new File("F:\\美女\\1.jpg");
    FileInputStream fileInputStream = new FileInputStream(file);

    int length = 0; 
    byte[] buf = new byte[1024]; 
    while((length = fileInputStream.read(buf))!=-1){ // 
        System.out.print(new String(buf,0,length));
    }

    fileInputStream.close();
}

存储读取到的数据 缓冲数组 的长度一般是1024的倍数,因为与计算机的处理单位。 理论上缓冲数组越大,效率越高

使用方法二和方法四。

输出字节流

FileOutputStream

OutputStream 是所有输出字节流 的父类。 抽象类

  • FileOutputStream 向文件输出数据的输出字节流。

FileOutputStream使用步骤:

  1. 找到目标文件
  2. 建立数据的输出通道。
  3. 把数据转换成字节数组写出。
  4. 关闭资源

FileOutputStream要注意的细节:

  • 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
  • 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
  • 使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
  • 使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是把低八位的二进制数据写出,其他二十四位数据全部丢弃。

写方法:

public class Demo {

    public static void main(String[] args) throws IOException {
        writeTest3();
    }


    //使用字节数组把数据写出。
    public static void writeTest3() throws IOException{
        File file = new File("F:\\b.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        String data = "abc";
        byte[] buf = data.getBytes();
        fileOutputStream.write(buf, 0, 3); // 0 从字节数组的指定索引值开始写, 2:写出两个字节。

        fileOutputStream.close();
    }



    //使用字节数组把数据写出。
    public static void writeTest2() throws IOException{
        File file = new File("F:\\b.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);

        String data = "\r\nhello world";
        fileOutputStream.write(data.getBytes());

        fileOutputStream.close();
    }



    //write()每次写一个字节
    public static void writeTest1() throws IOException{
        File file = new File("F:\\b.txt");//找到目标文件
        FileOutputStream fileOutputStream = new FileOutputStream(file);//建立数据的输出通道
        //把数据写出
        fileOutputStream.write('h');
        fileOutputStream.write('e');
        fileOutputStream.write('l');
        fileOutputStream.write('l');
        fileOutputStream.write('o');

        fileOutputStream.close();
    }
}

异常处理

public static void copyImage() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            // 找到目标文件
            File inFile = new File("F:\\美女\\1.jpg");
            File outFile = new File("E:\\1.jpg");
            // 建立输入输出通道
            fileInputStream = new FileInputStream(inFile);
            fileOutputStream = new FileOutputStream(outFile);
            // 建立缓冲数组,边读边写
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, length);
            }
        } catch (IOException e) {
            System.out.println("拷贝图片出错...");
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                    System.out.println("关闭输出流对象成功...");
                }
            } catch (IOException e) {
                System.out.println("关闭输出流资源失败...");
                throw new RuntimeException(e);
            } finally {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                        System.out.println("关闭输入流对象成功...");
                    } catch (IOException e) {
                        System.out.println("关闭输入流对象失败...");
                        throw new RuntimeException(e);
                    }
                }

            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值