JAVA 的IO操作(三) 字符输出流和输入流:Writer和Reader

来源:互联网 发布:数据库安全防范 编辑:程序博客网 时间:2024/05/16 10:37

一、字符输出流 Writer

在程序中一个字符等于两个字节。那么Java提供了Reader和Writer两个专门操作字符流的类。

Class Writer

  • java.lang.Object
    • java.io.Writer
  • All Implemented Interfaces:
    Closeable,Flushable, Appendable, AutoCloseable
    Direct Known Subclasses:
    BufferedWriter,CharArrayWriter, FilterWriter, OutputStreamWriter(有子类FileWriter), PipedWriter, PrintWriter, StringWriter
public abstract class Writerextends Objectimplements Appendable, Closeable, Flushable
Appendable接口实现可追加
Writerappend(char c)
Appends the specified character to this writer.
将指定字符添加到此 writer。
Writerappend(CharSequence csq)
Appends the specified character sequence to this writer.
将指定字符序列添加到此 writer。
Writerappend(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
 将指定字符序列的子序列添加到此 writer.Appendable
abstract voidclose()
Closes the stream, flushing it first.
关闭此流,但要先刷新它。
abstract voidflush()
Flushes the stream.
刷新该流的缓冲。
voidwrite(char[] cbuf)
Writes an array of characters.
写入字符数组。
abstract voidwrite(char[] cbuf, int off, int len)
Writes a portion of an array of characters.
写入字符数组的某一部分。
voidwrite(int c)
Writes a single character.
写入单个字符。
voidwrite(String str)
Writes a string.
写入字符串。
voidwrite(String str, int off, int len)
Writes a portion of a string.
写入字符串的某一部分。

列1:

package org.yts.iowriterreaderdemo;import java.io.File;import java.io.FileWriter;/* * 向文件中写入数据 */public class WriterDemo01 {public static void main(String[] args) throws Exception{File file = new File("e:" + File.separator + "test.txt");FileWriter out = new FileWriter(file);//FileWriter是OutputStreamWriter的子类String str = "Hello 中间有中文 World!!!";out.write(str);//使用.write(String str)直接写入字符串out.close();}}

列2:

package org.yts.iowriterreaderdemo;import java.io.File;import java.io.FileWriter;/* * 使用FileWriter追加文件的内容 */public class WriterDemo02 {public static void main(String[] args) throws Exception{File file = new File("e:" + File.separator + "test.txt");//使用FileWriter内构造new FileWriterFile file, boolean append)进行追加FileWriter out = new FileWriter(file,true);String str = "\r\n追加内容";//使用\r\n换行out.write(str);//使用.write(String str)直接写入字符串out.close();}}

二、字符输入流 Reader

Reader是使用字符的方式从文件中取出数据
Reader本身也是抽象类,如果现在要从文件中读取内容,则可以直接使用FileReader子类

Class Reader

  • java.lang.Object
    • java.io.Reader
  • All Implemented Interfaces:
    Closeable,AutoCloseable, Readable
    Direct Known Subclasses:
    BufferedReader,CharArrayReader, FilterReader, InputStreamReader(有其子类FileReader), PipedReader, StringReader
public abstract class Readerextends Objectimplements Readable, Closeable
abstract voidclose()
Closes the stream and releases any system resources associated with it.
关闭该流并释放与之关联的所有资源。
voidmark(int readAheadLimit)
Marks the present position in the stream.
标记流中的当前位置。
booleanmarkSupported()
Tells whether this stream supports the mark() operation.
判断此流是否支持 mark() 操作。
intread()
Reads a single character.
读取单个字符。
intread(char[] cbuf)
Reads characters into an array.
将字符读入数组。
abstract intread(char[] cbuf, int off, int len)
Reads characters into a portion of an array.
将字符读入数组的某一部分。
intread(CharBuffer target)
Attempts to read characters into the specified character buffer.
试图将字符读入指定的字符缓冲区。
booleanready()
Tells whether this stream is ready to be read.
判断是否准备读取此流。
voidreset()
Resets the stream.
重置该流。
longskip(long n)
Skips characters.
跳过字符。

列1:
package org.yts.iowriterreaderdemo;import java.io.File;import java.io.FileReader;/* * 从文件中读取内容, */public class ReaderDemo01 {public static void main(String[] args) throws Exception{File file = new File("e:" + File.separator + "test.txt");FileReader in = new FileReader(file);//开辟字符数组,用来存储读取的内容。char[] c = new char[1024];//使用.read(char[] c),返回的数据是读取的个数,相当于长度int len = in.read(c);in.close();System.out.println(new String(c,0,len));}}

列2:
package org.yts.iowriterreaderdemo;import java.io.File;import java.io.FileReader;/* * 使用循环的方式读取内容 */public class ReaderDemo02 {public static void main(String args[]) throws Exception{File file = new File("e:" + File.separator + "test.txt");FileReader in = new FileReader(file);char[] c = new char[1024];int len = 0;//用于记录读取的数据个数int temp = 0;//接收读取的每一个内容while((temp = in.read()) != -1){//将每次的读取的内容给temp变量,如果temp的值不是-1,则表示文件没有读完c[len] = (char) temp;len++;}in.close();System.out.println(new String(c,0,len));}}



0 0
原创粉丝点击