File和IO流(3)

字符流

字节流已可操作所有文件类型,为何还需要学习字符流:

  • 如果利用字节流,把文本文件中的中文,读取到内存中,有可能出现乱码。
  • 如果利用字节流,把中文写到文本文件中,也有可能出现乱码

重点:windows默认使用码表为:GBK,里面包含中文,并且一个中文字符两个字节

           idea和以后工作默认使用Unicode的UTF-8编解码格式,一个中文三个字节

编码:

        String c = "我是宇宙第一无敌超级暴龙战士";//14个中文字符
        byte[] bytes = c.getBytes();//括号内可写编码规则,如utf-8,GBK...
        System.out.println(Arrays.toString(bytes));//14*3共42

 解码:

        byte[] b = {-26, -120, -111, -26, -104, -81, -27, -82, -121, -27, -82, -103, -25, -84, -84, -28, -72, -128, -26, -105, -96, -26, -107, -116, -24, -74, -123, -25, -70, -89, -26, -102, -76, -23, -66, -103, -26, -120, -104, -27, -93, -85};
        byte[] b1 = {-26, -120, -111, -26, -104, -81, -27, -82, -121, -27, -82, -103, -25, -84, -84, -28, -72, -128, -26, -105, -96, -26, -107, -116, -24, -74, -123, -25, -70, -89, -26, -102, -76, -23, -66, -103, -26, -120, -104, -27, -93, -85};


        String a = new String(b);//默认使用UTF-8进行解码
        String a1 = new String(b1,"UTF-8");//指定使用UTF-8进行解码
        System.out.println(a);
        System.out.println(a1);

 字符流读取中文的过程:

字符流 = 字节流 + 编码表

不管实在哪张表中,中文的第一个字节一定是负数

小结:

  • 1、要进行拷贝,均使用字节流或者字节缓冲流
  • 2、想要把文本文件中的数据读到内存中,请使用字符输入流
  •       想要把内存中的数据写到文本文件中,请使用字符输出流

字符流写出数据

//文件不存在时会自动创建文件,但前提是保证父级文件夹是存在的

        FileWriter fw = new FileWriter("Bhpractice\\src\\aResumeitheima\\Day_10\\字符流\\字符流01.txt");

//        method1(fw);
//        method2(fw);
//        method3(fw); 重点
        method4(fw);

        fw.close();
    }

    private static void method4(FileWriter fw) throws IOException {
        String line = "究极无敌暴龙战士";
        fw.write(line,0,6);
    }

    private static void method3(FileWriter fw) throws IOException {
        String line= "究极无敌暴龙战士";
        fw.write(line);
    }

    private static void method2(FileWriter fw) throws IOException {
        char[] b = {97,98,99};
        fw.write(b,0,2);
    }

    private static void method1(FileWriter fw) throws IOException {
                char[] b = {97,98,99};
        fw.write(b);
    }

flush()刷新流,刷新完毕之后,还可以继续写数据
close()关闭流, 释放资源。一旦关闭,就不能写数据


字符缓冲流

注:输入流是从文件读取数据,是一个拉取数据的过程;输出流是将数据写入到文件,是一个推送数据的过程。

字符缓冲输入流:读取数据

        BufferedReader br = new BufferedReader(new FileReader("Bhpractice\\src\\aResumeitheima\\Day_10\\字符流\\u&p.txt"));

        char [] c = new char[1024];
        int len;
        while ((len = br.read(c))!=-1){
            System.out.println(c);
        }
        br.close();

字符缓冲输出流:写入数据

        BufferedWriter bw = new BufferedWriter(new FileWriter("Bhpractice\\src\\aResumeitheima\\Day_10\\字符流\\CSD7.txt"));

        bw.write(97);
        bw.write("\r\n");

        char[] c = {97,98,99,100,101,102};
        bw.write(c);
        bw.write("\r\n");

        bw.write(c,0,3);
        bw.write("\r\n");

        bw.write("测试中文");
        bw.write("\r\n");

        String line = "asdzsc";
        bw.write(line);

        bw.close();


缓冲流具有的两种特有方法:

字符缓冲输出流(BufferedWriter):newLine        (无视系统类型自动换行)

        BufferedWriter bw = new BufferedWriter(new FileWriter("Bhpractice\\src\\aResumeitheima\\Day_10\\字符流\\CSD89.txt"));
        bw.write("泽泽666");
        bw.newLine();
        bw.write("xiaoying-sixsixsix");
        bw.newLine();
        bw.write("666");

        bw.close();

字符缓冲输入流(BufferedReader):readLine        (读取整行)

        BufferedReader br = new BufferedReader(new FileReader("Bhpractice\\src\\aResumeitheima\\Day_10\\字符流\\CSD89.txt"));

        String s1 = br.readLine();
        String s2 = br.readLine();
        String s3 = br.readLine();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        br.close();
        BufferedReader br = new BufferedReader(new FileReader("Bhpractice\\src\\aResumeitheima\\Day_10\\字符流\\CSD89.txt"));

        String s;
        while ((s = br.readLine()) != null){
            System.out.println(s);
        }


        br.close();


小结:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值