Java IO(四--字符流基本使用

Posted huigelaile

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java IO(四--字符流基本使用相关的知识,希望对你有一定的参考价值。

在上一节,介绍了字节流的基本使用,本节介绍一下字符流的使用

技术图片

Reader:

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的方法相差不大,是定义字符输入流的抽象类

Writer:

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;	//关闭

FileReader、FileWriter:

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();

BufferedReader、BufferedWriter:

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()

  如果没有的话,缓冲区中的内容是无法输出的

以上是关于Java IO(四--字符流基本使用的主要内容,如果未能解决你的问题,请参考以下文章

IO流入门-第四部分-FileReader

读写文件的基本流都有哪些

转载JAVA中IO流操作的基本规律总结

java基础——流体系,字符流和字节流基本用法

Java IO 目录

Java IO流--使用FileReader读入数据的基本操作