JavaSE-IO流(1/2)

本文详细介绍了Java中的IO流,包括字节流 FileInputStream 和 FileOutputStream 的使用,以及在处理字节数据时如何确保文件完整性的讨论。此外,还探讨了ByteArrayInputStream 和 DataInputStream 在处理缓冲字节数据和基本类型数据时的角色。最后,展示了字节流和字符流之间的转换方法,以及使用 BufferedReader 和 BufferedWriter 进行文件读写操作的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IO流概述

  input-输入
  input-输出
流的概念:字节的序列
1.流的最小单位是字节
2.对于不同的序列,流也就不同

FileInputStream和FileOutputStream

    public static void main(String[] args) throws IOException {
        //实例化一个文件对象,在后续流的读取上有很大的作用
        File file = new File("D:\\2.txt");
        //先判断文件是否存在
        if (file.exists()) {
            System.out.println("该文件已经存在");
        } else
            //不存在就先用file的createNewFile去创建一个
            file.createNewFile();

        //在这里采取的是文件流之间的读写,所以这里便用到了 FileInputStream
        FileInputStream is = new FileInputStream("D:\\a12.txt");
        //在之前我们先实例了文件对象,并且这就是我们想放的地方
        FileOutputStream os = new FileOutputStream(file);
        //一次只能传输一些内容,而我们想要传输完所有的内容就自然而然的想到循环
        int i = 0;
        //而在最后没有内容的时候i会变成-1,这同样也就是循环的出口
        while (i != -1) {
            //实现读取
            i = is.read();
            //实现写
            os.write(i);
        }
        is.close();
        os.close();
        /**
         * 这一个例子实现简单的文件流的读取和写入比较需要注意的点就是在多次读取写入的过程中
         * 需要注意当读取搭配没有内容的时候i会返回-1 以此就可以来做循环的出口
         */
    }

在使用byte类型的读写的方式 会存在 如果文件是字节类型
比如音频 视频 图片 -----文件完整性是能够有保证的
但是如果是文字 字符型的数据 那么数组当中会因为最后一次读取的时候
Byte字节数不够1024个单位 数组当中会存有上次循环读取的数据
也会写入到文件中。

 public static void main(String[] args) throws IOException {
        File file = new File("D:\\3.txt");
        if (file.exists())
        {
            System.out.println("文件存在");
        }
        else
            file.createNewFile();
        FileInputStream is = new FileInputStream("D:\\a12.txt");
        FileOutputStream ot = new FileOutputStream(file);
        byte[] by = new byte[1024];           //在此创建了一个数组对象,每次以一个数组大小进行读和写
        int i = 0;
        while((i = is.read(by))!= -1)         //!在此处的判断条件非常重要!
        {
            ot.write(by,0,i);                //写操作
        }

        is.close();                         //关流也是不可缺少的
        ot.close();

    }

byteArrayInputStream和DataInputStream
ByteArrayInputStrem ----主要用于处理缓冲的字节数据
DataInputStream -----主要用于处理 处理缓冲字节数据数组 还用于处理基本数据类型的数据

    //    ByteArrayInputStrem ----主要用于处理缓冲的字节数据
//    DataInputStream -----主要用于处理 处理缓冲字节数据数组 还用于处理基本数据类型的
    public static void main(String[] args) throws IOException {
        //先写入数据
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        //DataOutputStream要和ByteArrayOutputStream连用
        DataOutputStream Do = new DataOutputStream(bo);
        //写入两个基本类型
        Do.writeDouble(3.14);
        Do.writeBoolean(false);

        ByteArrayInputStream bi =
                new ByteArrayInputStream(bo.toByteArray()); //在这里的这个构造方法必须放数组
        DataInputStream Di = new DataInputStream(bi);

        //记得此处的顺序!
        double d = Di.readDouble();
        Boolean b = Di.readBoolean();

        System.out.println(d);
        System.out.println(b);

    }

字节流和字符流转换
在这里插入图片描述

    public static void main(String[] args) throws IOException {
        //先放入一个目标文件
//        FileInputStream file = new FileInputStream("D:\\2.txt");
//        //转换到字符流
//        InputStreamReader isr = new InputStreamReader(file);
//        //缓冲
//        BufferedReader br = new BufferedReader(isr);

//        BufferedReader br = new BufferedReader(
//                                new InputStreamReader(
//                                    new FileInputStream("D://2.txt")));
// 两种方式 第一种代码量较长但是逻辑便于理解,第二种建议在熟练运用之后使用

        //读取文件
        FileInputStream file = new FileInputStream("D:\\2.txt");
        InputStreamReader isr = new InputStreamReader(file);
        BufferedReader br = new BufferedReader(isr);

        //写
        BufferedWriter bw =
                new BufferedWriter(
                        new OutputStreamWriter(
                                new FileOutputStream("E:\\3.txt")));

        String result = "";                          //用来放

        StringBuffer sb = new StringBuffer();        //用来显示我们写入了什么

        while((result = br.readLine())!=null){       //注意此处的判断条件是空不是-1了
            // 写的操作
            bw.write(result);                        //写
            bw.newLine();                           //换行
            sb.append(result+"\n");
        }

        System.out.println(sb.toString());          //输出

        bw.flush();
        br.close();
        bw.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值