java Core Programming: Character Stream of IO Stream (3)

1. character stream

1.1 All files are stored in bytes. What we usually save on disk is not the characters of files, but the characters are converted into bytes first, and then stored on disk. When reading a file, it is also a byte-by-byte read, and then generates a sequence of bytes.

1.2 Byte streams can handle any object, but character streams, only characters or strings. It can be said that byte stream provides the most basic IO function to handle any IO operation, but one point is that Unicode characters can not be directly processed. Why, because Unicode characters are a unit of two bytes, while byte stream processing unit is a byte, character stream is converted by Java virtual machine into two bytes of Unicode characters as a unit. Character formation. If you use byte stream to process Unicode characters directly, it is easy to get scrambled code because of different encoding implementations.

In 1.3 example, a lot of data we deal with everyday is text, so we put forward the concept of character stream besides byte stream, which is directly processed according to the encode of Java virtual machine, that is, character set conversion between output stream and input stream.

2.java Character Stream Implementation Architecture

1. Input character stream

2. Output character stream

  

3. Common Classes of Character Stream

Char Array Reader and Char Array Writer

package se.io;

import com.sun.org.apache.xpath.internal.operations.String;

import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayTest {

    public static void main(String[] args) {

        char[] chars = new char[3];
        chars[0] = 100;
        chars[1] = 101;
        chars[2] = 102;

        CharArrayReader charArrayReader = new CharArrayReader(chars);
        try {
            char[] buf = new char[1024];
            charArrayReader.read(buf);

            CharArrayWriter charArrayWriter = new CharArrayWriter();
            charArrayWriter.write(buf);

            System.out.println(charArrayWriter.toString());
            charArrayWriter.close();
            charArrayReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}

3.2 File Input and Output Streams (FileWriter and FileReader)

package se.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileCharTest {

    public static void main(String[] args) {

        try {
            FileReader fileReader = new FileReader("E:\\test\\data.txt");
            char[] chars = new char[1024];
            int i = fileReader.read(chars);

            FileWriter fileWriter = new FileWriter("E:\\test\\filewriter.txt");
            fileWriter.write(chars,0,i);

            fileWriter.close();
            fileReader.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

3.3 PipeReader and PipeWriter

package se.io;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedChar {

    public static void main(String[] args) {

        SenderChar senderChar = new SenderChar();
        PipedWriter pipedWriter = senderChar.getWriter();

        ReceiverChar receiverChar = new ReceiverChar();
        PipedReader pipedReader =receiverChar.getReader();
        try {
            pipedWriter.connect(pipedReader);

            senderChar.start();
            receiverChar.start();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}


//Create sender class
class SenderChar extends Thread{
    private PipedWriter writer = new PipedWriter();

    public PipedWriter getWriter() {
        return writer;
    }

    public void run(){

        String s = new String("hello,world");
        try {
            writer.write(s);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
//Create recipients
class ReceiverChar extends  Thread{

    private PipedReader reader = new PipedReader();

    public PipedReader getReader() {
        return reader;
    }

    public  void run(){

        String s= null;
        char[] chars = new char[1024];

        try {
            int i = reader.read(chars);
            s = new String(chars,0,i);
            System.out.println(s);
            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.4 Cache Input and Output Streams (Buffered Reader and Buffered Writer)

package se.io;

import java.io.*;

public class BufferWriterTest {

    public static void main(String[] args) {

        try {

            //Construct the input and output character stream of the file, and automatically generate the specified file if it does not exist.
            FileReader fileReader = new FileReader("E:\\test\\data.txt");
            FileWriter  fileWriter = new FileWriter("E:\\test\\data2.txt");

            //Constructing Filtration Cache Stream
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            //Cache array
            char[] chars = new char[1024];
            int offset = 0 ;

            //Read character
            while(bufferedReader.ready()) {
                offset = bufferedReader.read(chars);
            }
            //Output character
            bufferedWriter.write(chars,0,offset);

            //Closed flow
            bufferedWriter.close();
            bufferedReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

3.5 Formatted Output Stream (Printer Writer)

package se.io;

import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class PrintWriterTest {

    public static void main(String[] args) {


        try {
            //Create a file output stream
            FileWriter fileWriter = new FileWriter("E:\\test\\data3.txt");
            //Create a formatted object output stream
            PrintWriter printWriter = new PrintWriter(fileWriter);

            //Write data
            printWriter.printf("%1$tY year%1$tm month%1$td day", new Date());
            printWriter.print(123);
            printWriter.print(12.32);
            //Closed flow
            printWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Keywords: Java encoding Apache

Added by sciencebear on Sat, 25 May 2019 21:25:12 +0300