什么是字符流?为什么会有字符流?

为什么出现字符流?因为字节流操作中文不是特别方便,所以Java就提供字符流;字符流=字节流+编码表;字节流复制文本文件时也会有中文,但是没有问题,原因是最终底层操作会自动进行字节拼接成中文,如何识别中文呢?是因为汉字在存储时无论选择哪种编码存储,第一个字节都是负数。

字符流写数据的五种方式:

1.void write(int c); 写一个字符

public class demo {
    public static void main(String[] args) throws IOException {
        //FileInputStream fis = new FileInputStream("D:\\wkx\\shangluo");
        FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\.chengzi.txt");
        OutputStreamWriter ow = new OutputStreamWriter(fos);
        ow.write(97);
        ow.flush();
        ow.write(98);
        ow.close();
    }
}

2.void write(char[] cbuf); 写一个字符数组

public class demo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\.chengzi.txt");
        OutputStreamWriter ow = new OutputStreamWriter(fos);
        char [] ch ={97,98,99,100};
        ow.write(ch);
        ow.close();
    }
}

3.void write(char[] cbuf,int off, int len);写一个字符数组的一部分

public class demo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\.chengzi.txt");
        OutputStreamWriter ow = new OutputStreamWriter(fos);
        char [] ch ={97,98,99,100};
        ow.write(ch,0,2);
        ow.close();
    }
}

4.void write(String str);写一个字符串

public class demo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\.chengzi.txt");
        OutputStreamWriter ow = new OutputStreamWriter(fos);
       ow.write("chengzi");
       ow.close();
    }
}

5.void write(String str,int off , int len);写一个字符串的一部分

public class demo {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\.chengzi.txt");
        OutputStreamWriter ow = new OutputStreamWriter(fos);
       ow.write("chengzi",0,5);
       ow.close();
    }
}

flush方法和close方法的区别:close释放资源前进行一次刷新流,close执行后不能在写内容;而flush是刷新流,执行完毕依旧可以写内容;

字符流读数据两种方式:

1.int read();一次读取一个字符数据

public class demo {
    public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("D:\\wkx\\shangluo\\.chengzi.txt");
       InputStreamReader ir = new InputStreamReader(fis);
       int ch;
       while ((ch=ir.read())!=-1){
           System.out.print((char)ch);
       }
    }
}

2.int read(char[] cbuf);一次读取一个字符数组数据

public class demo {
    public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("D:\\wkx\\shangluo\\.chengzi.txt");
       InputStreamReader ir = new InputStreamReader(fis);
       char [] cha = new char[1024];
       int len;
       while ((len=ir.read(cha))!=-1){
           System.out.print(new String (cha,0,len));
       }
    }
}

字符流复制文件案例:

public class demo {
    public static void main(String[] args) throws IOException {
        InputStreamReader sr = new InputStreamReader(new FileInputStream("D:\\wkx\\shangluo\\.chengzi.txt"));
        OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream("D:\\wkx\\shangluo\\橙子.txt"));
        char [] ch = new char[1024];
        int len;
        while ((len=sr.read(ch))!=-1){
            os.write(new String(ch,0,len));
        }
        sr.close();
        os.close();
    }
}

简洁:

public class demo {
    public static void main(String[] args) throws IOException {
        FileReader sr = new FileReader("D:\\wkx\\shangluo\\.chengzi.txt");
        FileWriter os = new FileWriter("D:\\wkx\\shangluo\\橙子.txt");
        int ch;
        while ((ch=sr.read())!=-1){
            os.write((char)ch);
        }
        sr.close();
        os.close();
    }
}

字符缓冲流:

BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或使用默认,默认可用于大多数用途;

BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或使用默认,默认可用于大多数用途;

构造方法:BufferedWriter(Writer out);      BufferedReader(Reader  in);

public class demo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\wkx\\shangluo\\.chengzi.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\wkx\\shangluo\\橙子.txt"));
        char [] ar = new char[1024];
        int len;
        while ((len=br.read(ar))!=-1){
            bw.write(new String(ar,0,len));
        }
        br.close();
        bw.close();
    }
}

使用缓冲流复制文件效率更高;

字符缓冲流特有功能:

BufferedWriter:   void   newLine():写一行行分隔符,行分隔符字符串由系统属性定义

BufferedReader: public String  readLine():读一行文字,结果包含行的内容的字符串,不包含任何终止字符,如果流的结尾已经到达,则为null

public class demo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\wkx\\shangluo\\.chengzi.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\wkx\\shangluo\\橙子.txt"));

        String len;
        while ((len=br.readLine())!=null){
           bw.write(len);
           bw.newLine();
           bw.flush();
        }
        br.close();
        bw.close();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值