标签:抽象 ring 行数据 family txt 缓冲区 子序列 long sha
在上一节,介绍了字节流的基本使用,本节介绍一下字符流的使用
public abstract class Reader implements Readable, Closeable { protected Object lock; private static final int maxSkipBufferSize = 8192; private char[] skipBuffer = null; protected Reader() { this.lock = this; } protected Reader(Object var1) { } public int read(CharBuffer var1) throws IOException { } //试图将文件中的字符读入指定的字符缓冲区 public int read() throws IOException { } //从文件中读取单个字符 public int read(char[] var1) throws IOException { } //从文件中读取字符到var1 public abstract int read(char[] var1, int var2, int var3) throws IOException; //将文件中的字符读入cbuf数组,从off位置开始,读取len个字符 public long skip(long var1) throws IOException { } //跳过var1个字符 public boolean ready() throws IOException { } //判断是否准备读取此流 public boolean markSupported() { } //判断此流是否支持mark()操作 public void mark(int var1) throws IOException { } //标记流中的当前位置 public void reset() throws IOException { } //重置 public abstract void close() throws IOException; }
我们可以发现Reader和InputStream的方法相差不大,是定义字符输入流的抽象类
public abstract class Writer implements Appendable, Closeable, Flushable { private char[] writeBuffer; private static final int WRITE_BUFFER_SIZE = 1024; protected Object lock; protected Writer() { this.lock = this; } protected Writer(Object var1) { } public void write(int var1) throws IOException { } //写入单个字符到文件中 public void write(char[] var1) throws IOException { } //将char[] var1中的内容写入文件 public abstract void write(char[] var1, int off, int len) throws IOException; //将字符数组var1中从off开始的len个字节写入文件 public void write(String var1) throws IOException { } //将字符串var1写入到文件中 public void write(String var1, int off, int len) throws IOException { } //写入var1从off位置开始的len个字符到文件中 public Writer append(CharSequence var1) throws IOException { } //将制定字符序列添加到此writer public Writer append(CharSequence var1, int var2, int var3) throws IOException { } //将指定字符序列的子序列添加到此writer public Writer append(char var1) throws IOException { } //将制定字符添加到此writer public abstract void flush() throws IOException; //强制提交,刷新该流的缓冲 public abstract void close() throws IOException; //关闭 }
public static void main(String[] args) throws IOException { String pathName = "D:" + File.separator + "a.txt"; FileWriter fileWriter = new FileWriter(pathName); fileWriter.write("abcabcabcabcabcabc".toCharArray()); fileWriter.close(); FileReader fileReader = new FileReader(pathName); char[] chars = new char[1024]; fileReader.read(chars); for (char c : chars) { System.out.print(c); } fileReader.close(); }
public static void main(String[] args) throws IOException { String pathName = "D:" + File.separator + "a.txt"; BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(pathName)); bufferedWriter.write("abcabcabcabcabcabc".toCharArray()); bufferedWriter.newLine(); //跳到下一行 bufferedWriter.write("next line".toCharArray()); bufferedWriter.close(); BufferedReader bufferedReader = new BufferedReader(new FileReader(pathName)); char[] chars = new char[1024]; bufferedReader.read(chars); for (char c : chars) { System.out.print(c); } System.out.println(bufferedReader.readLine()); //读取一行数据 bufferedReader.close(); }
新增了newLine()和readLine()
至于CharArrayReader和CharArrayWrite、PipedReader和PipedWriter等和InputStream、OutputStream中子类使用都是相似的,自行类比
字节流在操作时本身不会用到缓冲区,是对文件本身直接操作的
字符流操作时使用了缓冲区,通过缓冲区再操作文件,要使用flush()或者close()
如果没有的话,缓冲区中的内容是无法输出的
标签:抽象 ring 行数据 family txt 缓冲区 子序列 long sha
原文地址:https://www.cnblogs.com/huigelaile/p/11051837.html