IO streams of the character stream

Character stream

    (1) Operation byte stream data is not particularly convenient Chinese, so there have been converted stream.

       Role is to convert the flow of the byte stream into a stream of characters to use.

    (2) converting the stream is actually a character stream

        = + Character stream code table byte stream

    (3) code table

        A: is a numeric value corresponding to the characters and a table consisting of

        B: common coding table

            ASCII

            ISO-8859-1

            GB2312

            GBK

            GB18030

            UTF-8

        C: coding problem string

            Code: to become able to understand not read

                String -- byte[]

            Decoding: can not read into the can understand

                byte[] -- String


 

/*

 * String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组

 * byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组

 *

 * 编码:把看得懂的变成看不懂的

 * String -- byte[]

 *

 * 解码:把看不懂的变成看得懂的

 * byte[] -- String

 *

 * 要发送一段文字:

 *         今天晚上在老地方见

 *

 *         发送端:今 -- 数值 -- 二进制 -- 发出去

 *         接收端:接收 -- 二进制 -- 十进制 -- 数值 -- 字符 -- 今

 *

 *         今天晚上在老地方见

 */

public class StringDemo {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String s = "你好";

        //String-byte[]

        byte[] bys = s.getBytes(); // [-60, -29, -70, -61]

        System.out.println(Arrays.toString(bys));



        // byte[] -- String

        String ss = new String(bys); // 你好

        System.out.println(ss);

    }

}

    (4) IO issues and stream encoding method

        A:OutputStreamWriter

            OutputStreamWriter (OutputStream os): The default encoding, GBK converting data character stream is a stream of bytes

            OutputStreamWriter (OutputStream os, String charsetName): according to the specified coding. Converting data character stream is a stream of bytes

           method:

               public void write (int c): write a character

               public void write (char [] cbuf): Write an array of characters

               Part of writing a character array: public void write (char [] cbuf, int off, int len)

               public void write (String str): write a string

               public void write (String str, int off, int len): Write part of a string

            Interview questions: the difference between close () and flush () is?

               A: close () Closes the stream object, but first refresh buffer. After closing, the flow must not continue to be used again.

               B: flush () only flushes the buffer after the refresh, the stream object can continue to use.

        B:InputStreamReader

            InputStreamReader (InputStream is): the default encoding, GBK

            InputStreamReader (InputStream is, String charsetName): specify the encoding

          method:

               int read (): reads one character

               int read (char [] chs): reads one character array

        C: coding problem is very simple

            As long as you can encode the same

    (5) the character stream

        Reader

            |--InputStreamReader

                |--FileReader

            |--BufferedReader

                    public String readLine (): read a line of data: a string that contains the contents of the line, not including any line terminator, if the end of the stream is  

                                                              null

        Writer

            |--OutputStreamWriter

                |--FileWriter

            |--BufferedWriter

                    public void newLine (): The system determines newline

    (6) Copy the text file

          

/*

 * 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中

 *

 * 数据源:

 *         a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader

 * 目的地:

 *         b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter

 */

public class CopyFileDemo2 {

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

        // 封装数据源

        BufferedReader br = new BufferedReader(new FileReader("a.txt"));

        // 封装目的地

        BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));



        // 方式1:一次读写一个字符数组

        //char[] chs = new char[1024];

        //int len = 0;

        //while ((len = br.read(chs)) != -1) {

            //bw.write(chs, 0, len);

            //bw.flush();

        //}



        //方式2:一次读取一行数据

        String line = null;

        while ((line = br.readLine()) != null) {

            bw.write(line);

            bw.newLine();

            bw.flush();

        }



        // 释放资源

        bw.close();

        br.close();

    }

}

 

Published 114 original articles · won praise 52 · views 20000 +

Guess you like

Origin blog.csdn.net/Smile_Sunny521/article/details/89703926