2023/3/2 IO(第一部分)

IO流

一、IO流的分类:

1、根据流的方向:

①输入流:读取
②输出流:写入

2、根据操作文件的类型:

①字节流:适用于所有类型的文件
②字符流:使用于纯文本文件

请添加图片描述

二、字节输出流

请添加图片描述

1、write方法的重载:

①write(int b)
②write(byte[] b)
③write(byte[] b, int off, int len)

2、换行与续写

①换行:Windows中:\r\n
Linux中:\n
Max中:\r
public class Test1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("day26-code\\aaa\\a.txt");
        byte[] b1 = "woshidashuaige".getBytes();
        fos.write(b1);

        byte[] b2 = "\r\n".getBytes();
        fos.write(b2);

        byte[] b3 = "niyeshi".getBytes();
        fos.write(b3);

        fos.close();
    }
}
②续写:

请添加图片描述

        FileOutputStream fos = new FileOutputStream("day26-code\\aaa\\a.txt", true);

三、字节输入流

请添加图片描述

1、循环读取数据

public class Test2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("day26-code\\aaa\\a.txt");
        int b;
        while ((b = fis.read()) != -1) {
            System.out.print((char) b);
        }
        fis.close();
    }
}

2、拷贝数据

public class Test3 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("day26-code\\aaa\\a.txt");
        FileOutputStream fos = new FileOutputStream("day26-code\\aaa\\b.txt");
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b);
        }
        fos.close();
        fis.close();
    }
}

3、拷贝数据缓冲池优化

public class Test4 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("day26-code\\aaa\\a.txt");
        FileOutputStream fos = new FileOutputStream("day26-code\\aaa\\b.txt");
        int len;
        /*使用数组缓冲,加快传输速度*/
        byte[] bytes = new byte[8];
        /*判断条件还是不等于-1
        * len表示的是这次read改变了缓冲池bytes中多少长度*/
        while ((len = fis.read(bytes)) != -1) {
            /*只写入改变了的元素
            * 解释:可能最后一次读取,缓冲池并未被完全覆盖,只有前面几个元素被覆盖
            * 此时,被改变的元素个数赋值给了len,根据len的大小进行写可以保证写无误*/
            fos.write(bytes, 0, len);
        }
        fos.close();
        fis.close();
    }
}

4、不同版本JDK通过try——catch处理异常

请添加图片描述
请添加图片描述

四、字符集

1、GBK编码

请添加图片描述

2、Unicode编码

请添加图片描述

UTF-8是Unicode字符集的一种编码方式,并非是字符集,注意区分

3、如何避免乱码

请添加图片描述

4、编码与解码

请添加图片描述

五、字符输入流

请添加图片描述

public class Test5 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("day26-code\\aaa\\a.txt");
        int ch;
        /*字符流一次读入一个字符,但是是用十进制整数表示的编码*/
        while ((ch = fr.read()) != -1) {
            /*通过强转,显示字符编码表示的内容*/
            System.out.print((char) ch);
        }
        fr.close();
    }
}
public class Test5 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("day26-code\\aaa\\a.txt");
        int len;
        /*改进,使用数组缓冲池
        * 注意和字节流的区别:这里用的是char数组,那边是byte数组*/
        char[] chars = new char[8];
        while ((len = fr.read(chars)) != -1) {
            System.out.print(new String(chars, 0, len));
        }
        fr.close();
    }
}

注意改进的方法里,read带参,通过数组默认进行了强转操作

六、字符输出流

请添加图片描述

1、方法

请添加图片描述

public class Test6 {
    public static void main(String[] args) throws IOException {
//        FileWriter fw = new FileWriter("day26-code\\aaa\\b.txt");
        FileWriter fw = new FileWriter("day26-code\\aaa\\b.txt", true);
        fw.write("\r\n");
        fw.write("你也是一只猪!");
        fw.close();
    }
}

2、字符流是存在缓冲区的(区别于自己定义的数组缓冲区,该缓冲区是存在于内存当中,用于加快IO速度的)

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值