IO---character stream

Character stream
***
Character stream = byte stream + encoding table (a table composed of characters and their corresponding values, the characters of each country are represented by numbers, and one-to-one correspondence to form a table)

Common coding table

  • ASCII/Unicode character set
  • ISO-8859-1
  • GB2312/GBK/GB18030
  • BIG5
  • UTF-8

character input stream

    public abstract class Reader implements Readable, Closeable
  • method

    abstract  void close()                              //关闭该流并释放与之关联的所有资源。 
    void mark(int readAheadLimit)                       //标记流中的当前位置。 
    boolean markSupported()                             //判断此流是否支持 mark() 操作。 
    int read()                                          //读取单个字符。 
    int read(char[] cbuf)                               //将字符读入数组。 
    abstract  int read(char[] cbuf, int off, int len)   //将字符读入数组的某一部分。 
    int read(CharBuffer target)                         //试图将字符读入指定的字符缓冲区。 
    boolean ready()                                     //判断是否准备读取此流。 
    void reset()                                        //重置该流。 
    long skip(long n)                                   //跳过字符。 

FileReader

    public class FileReader extends InputStreamReader

eg

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class RWTest {
    
    public static void main(String[] args) throws IOException {
        Reader in = new FileReader("D:/blog/test.txt");
        int len = -1;
        while((len = in.read())!=-1){
            System.out.print((char)len);
        }
        in.close();
    }
}

character output stream

    public abstract class Writer implements Appendable, Closeable, Flushable
  • method

    Writer append(char c)                                   //将指定字符添加到此 writer。 
    Writer append(CharSequence csq)                         //将指定字符序列添加到此 writer。 
    Writer append(CharSequence csq, int start, int end)     //将指定字符序列的子序列添加到此 writer.Appendable。 
    abstract  void close()                                  //关闭此流,但要先刷新它。 
    abstract  void flush()                                  //刷新该流的缓冲。 
    void write(char[] cbuf)                                 //写入字符数组。 
    abstract  void write(char[] cbuf, int off, int len)     //写入字符数组的某一部分。 
    void write(int c)                                       //写入单个字符。 
    void write(String str)                                  //写入字符串。 
    void write(String str, int off, int len)                //写入字符串的某一部分。 

FileWriter

    public class FileWriter extends OutputStreamWriter

In the construction method, FileWriter(String fileName, boolean append) append defaults to false, which means that the written data is appended to the existing data, and false means that the original data is overwritten.

eg

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class RWTest {
    
    public static void main(String[] args) throws IOException {
        Writer out = new FileWriter("d:/blog/test.txt");
        //Writer out = new FileWriter("d:/blog/test.txt",true);
        out.write("6666");
        out.close();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324783148&siteId=291194637