Java IO stream character stream and conversion stream

Keywords: encoding

Character stream

The reading of a character character can only be used to manipulate text (no other format can be written)

Character output stream (Writer)

Abstract class for writing character stream

Implement the subclass FileWriter
Code example:

public class Demo04 {
    public static void main(String[] args) throws IOException {
        FileWriter fWriter = new FileWriter("/Users/james/Desktop/level/haha.txt");
        fWriter.write(100);
        // Note that the character output stream needs to call the refresh method when writing to the file
        // It is recommended to refresh every write
        fWriter.flush();
        char[] c = {'l','o','n','g'};
        fWriter.write(c);
        fWriter.flush();
        fWriter.write(c, 1,3);
        fWriter.flush();
        // Write directly with string
        fWriter.write("Draw a knife, cut off the water, make the water more flowing\n");
        fWriter.flush();
        fWriter.write("To drink away sorrow is more sorrow\n");
        fWriter.flush();
        fWriter.write("A hundred days on the mountain", 1, 2);
        // Refresh before closing resources
        fWriter.close();
    }
}

Character input stream (Reader)

Abstract class for reading character stream

Implementation subclass: FileReader
Code example:

public class Demo05 {
    public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader("/Users/james/Desktop/level/haha.txt");
        // Circular reading
        char[] bs = new char[1024];
        int len = 0;
        while ((len = reader.read(bs)) != -1) {
            System.out.println(new String(bs, 0, len));
        }
        reader.close();
    }
}

In character stream, string type can be written directly in output stream, but only input stream. Why?

Because string types are difficult to determine when to end, the input stream cannot read the string directly

Example contact:

Using character stream to copy files

public class Demo06 {
    public static void main(String[] args) {

        File file = new File("/Users/james/Desktop/level/haha.txt");
        File file2 = new File("/Users/james/Desktop/level/ha.txt");
        copyFileTxt(file, file2);

    }

    // Character stream copy file
    public static void copyFileTxt(File src ,File dest) {
        FileReader reader = null;
        FileWriter writer = null;

        try {
            reader = new FileReader(src);
            writer = new FileWriter(dest);
            char[] c = new char[1024];
            int len = 0;
            while ((len = reader.read(c)) != -1) {
                writer.write(c, 0, len);
            }               
        } catch (FileNotFoundException e) {
            throw new RuntimeException("cannot find file");
        } catch (IOException e) {
            throw new RuntimeException("Compile exception");
        }finally {
            try {
                if (reader != null) {
                    reader.close();
                }               
            } catch (IOException e) {
                throw new RuntimeException("Resource close exception");
            }finally {
                try {
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("Resource close exception");
                }
            }
        }
    }
}

Conversion flow

Outputstreamwriter (character stream to byte stream)

Character stream to byte stream can use different encoding formats to write to FileOutStream class

Inputstreamreader (byte stream to character stream)

You need to use FileInputStream class to read files of different encoding formats

Code example:

public class Demo07 {
    public static void main(String[] args) throws IOException {
        //getUTF8();
//      getGBK();
//      readUTF8();
        readGBK();
    }

    // Write using the default UTF-8 of OutputStreamWriter
    public static void getUTF8() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/utf-8.txt");
        // Create conversion stream character stream to byte stream
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        // Write file
        osw.write("Snow in wisdom, rain in Apricot,Under the cherry tree, in the flower field");
        // Close resources (only the outer flow can be closed)
        osw.close();

    }
    // Using the conversion stream to write the file OutputStreamWriter using GBK
    public static void getGBK() throws IOException {
            FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/GBK.txt");
        // Build transform stream in encoding format (strings in encoding format ignore case)
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        osw.write("Snow in wisdom, rain in Apricot,Under the cherry tree, in the flower field");
        osw.close();    
    }

    public static void readUTF8() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/utf-8.txt");
        InputStreamReader isr = new InputStreamReader(fis); 
        int len = 0;
        char[] b = new char[1024];
        while ((len = isr.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }

        isr.close();
    }

    public static void readGBK() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/GBK.txt");
        InputStreamReader isr = new InputStreamReader(fis,"GBK"); 
        int len = 0;
        char[] b = new char[1024];
        while ((len = isr.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        isr.close();
    }
}

Posted by stebut05 on Wed, 29 Apr 2020 23:02:12 -0700