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接口实现可追加
Writer
append(char c)
Appends the specified character to this writer.
将指定字符添加到此 writer。
Writer
append(CharSequence csq)
Appends the specified character sequence to this writer.
将指定字符序列添加到此 writer。
Writer
append(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
将指定字符序列的子序列添加到此 writer.Appendable。
abstract void
close()
Closes the stream, flushing it first.
关闭此流,但要先刷新它。
abstract void
flush()
Flushes the stream.
刷新该流的缓冲。
void
write(char[] cbuf)
Writes an array of characters.
写入字符数组。
abstract void
write(char[] cbuf, int off, int len)
Writes a portion of an array of characters.
写入字符数组的某一部分。
void
write(int c)
Writes a single character.
写入单个字符。
void
write(String str)
Writes a string.
写入字符串。
void
write(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();}}
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 void
close()
Closes the stream and releases any system resources associated with it.
关闭该流并释放与之关联的所有资源。
void
mark(int readAheadLimit)
Marks the present position in the stream.
标记流中的当前位置。
boolean
markSupported()
Tells whether this stream supports the mark() operation.
判断此流是否支持 mark() 操作。
int
read()
Reads a single character.
读取单个字符。
int
read(char[] cbuf)
Reads characters into an array.
将字符读入数组。
abstract int
read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.
将字符读入数组的某一部分。
int
read(CharBuffer target)
Attempts to read characters into the specified character buffer.
试图将字符读入指定的字符缓冲区。
boolean
ready()
Tells whether this stream is ready to be read.
判断是否准备读取此流。
void
reset()
Resets the stream.
重置该流。
long
skip(long n)
Skips characters.
跳过字符。
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));}}
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
- JAVA 的IO操作(三) 字符输出流和输入流:Writer和Reader
- Java IO 字符流 Reader和Writer
- IO 输入与输出(3) -- 节点流之三Reader和Writer类
- Java-IO之字符输入输出流(Reader和Writer)
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- Java的IO操作(五) - PrintStream类、字符流、Reader和Writer、带缓冲的字符流BufferedReader和BufferedWriter
- Java的IO操作(五) - PrintStream类、字符流、Reader和Writer、带缓冲的字符流BufferedReader和BufferedWriter .
- Java中的字符流Reader和Writer
- Java知识(字符流Reader和Writer)
- 学习笔记之java.io包中的字符流(上)—— Reader和Writer
- 字符流Reader和Writer
- Java基础--------(7)IO流Reader和Writer
- Java - IO - 字符流 - Writer - Reader
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
- Java IO(十二):Reader 和 Writer
- IO流之----字符流(Reader和Writer)和其它流
- I学霸官方免费教程四十三 :Java流之字符流 Reader和Writer
- 关于ZM问到C中int数据范围溢出
- read读取管道
- ZeroMQ(java)中的数据流SessionBase与SocketBase
- java 笔记------instanceof、抽象、接口的应用
- 【剑指offer】链表相关-逆向输出
- JAVA 的IO操作(三) 字符输出流和输入流:Writer和Reader
- 2014年2月28日
- 黑马程序员_总结11-java 多线程
- ZeroMQ(java)中监控Socket
- 2014年2月28日
- 2014年2月28日
- 2014年2月28日
- 素数判定 - 素数打表
- 啊!社会真难啊!