Java conversion flow

Under normal circumstances, it can be a byte stream for all data manipulation, but sometimes when we use to deal with some text character stream, for example, when viewing text of Chinese characters is the need to adopt a more convenient stream. So Java IO stream provides two used to convert a stream of bytes to convert the character stream flow.

InputStreamReader for byte input stream into a character stream input, wherein the output stream of bytes for OutputStreamWriter converted to character output stream . Use commutations distortion can be avoided to some extent, you may also specify the character set used for input and output.

The output java.txt "Baidu search engine" six words, java.txt saved as "UTF-8" format, and then reads the byte stream by means of the following code:

public static void main(String[] args) {
    try {
        FileInputStream fis = new FileInputStream("D://java.txt");
        int b = 0;
        while ((b = fis.read()) != -1) {
            System.out.print((char) b);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The output is ???, we found that Chinese are garbled. The following array of bytes, and explicitly incorporated by setting a string encoding format, as follows:

public static void main(String[] args) {
    try {
        FileInputStream fis = new FileInputStream("D://java.txt");
        byte b[] = new byte[1024];
        int len = 0;
        while ((len = fis.read(b)) != -1) {
            System.out.print(new String(b, 0, len, "UTF-8"));
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

At this output is Baidu search engine, but when a large storage of text, there will not decode the right questions, and the byte length can not be set automatically according to decode the content, then you need to complete the conversion flow. code show as below:

public static void main(String[] args) {
    try {
        FileInputStream fis = new FileInputStream("D://java.txt");
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        int b = 0;
        while ((b = isr.read()) != -1) {
            System.out.print((char) b);    // 输出结果为“百度搜索引擎”
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

For example to introduce the use of keyboard input stream conversion . Java uses System.in represent standard output, i.e. keyboard input, but this is an example of the standard input stream InputStream class, less convenient to use, but also keyboard input content is text content, it is possible to convert it to the InputStreamReader character input stream, when ordinary Reader reads the input content is still not very convenient, you can be normal again Reader packaged into BufferedReader, use the readLine BufferedReader () method to read a single line. As shown in the following procedure.

public static void main(String[] args) {
    try {
        // 将 System.in 对象转换成 Reader 对象
        InputStreamReader reader = new InputStreamReader(System.in);
        // 将普通的Reader 包装成 BufferedReader
        BufferedReader br = new BufferedReader(reader);
        String line = null;
        // 利用循环方式来逐行的读取
        while ((line = br.readLine()) != null) {
            // 如果读取的字符串为“exit”,则程序退出
            if (line.equals("exit")) {
                System.exit(1);
            }
            // 打印读取的内容
            System.out.println("输入内容为:" + line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The above code lines 4 and 6 rows System.in packaged BufferedReader, BufferReader stream having a buffering function, it can read a line of text, marked by line breaks, line breaks if it is not read, the routine blocked until the read until newline. Run the above program may find this feature in the console to perform input, only press the Enter key, the program will print out the content just entered.

Since the method having the readLine BufferedReader (), can easily be read in a single line, so often the text is read into the input stream BufferedReader packaging, easy to read text input stream.

Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104711113