Java I/O 教程(八) Writer和Reader
Java Writer
Writer是一個用於寫字元流的抽象類。其子類必須實現write(char[], int, int), flush(), 和 close()方法。
類定義
public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable
屬性
Modifier and Type Field Description
protected Object lock The object used to synchronize operations on this stream.
建構函式
protected Writer()Creates a new character-stream writer whose critical sections will synchronize on the writer itself.
常用方法
Writer append(char c) 往writer追加指定字元Writer append(CharSequence csq) 追加指定字元序列
Writer append(CharSequence csq, int start, int end) 追加指定字元序列的子序列
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) 寫部分字串
例子
package com.dylan.io;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @author xusucheng
* @create 2018-01-07
**/
public class WriterExample {
public static void main(String[] args) throws IOException {
Writer w = new FileWriter("D:\\output.txt");
String content = "I love my country.";
w.write(content);
w.close();
System.out.println("Done.");
}
}
Java Reader
Reader 是一個用於讀取字元流的抽象類。其子類必須實現的方法只有:read(char[], int, int) 和 close()。
實現類包括: BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader
類定義
public abstract class Reader
extends Object
implements Readable, Closeable
屬性
protected Object |
lock
The object used to synchronize operations on this stream.
|
建構函式
protected |
Reader()
Creates a new character-stream reader whose critical sections will synchronize on the reader itself.
|
protected |
Reader(Object lock)
Creates a new character-stream reader whose critical sections will synchronize on the given object.
|
常用方法
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.
|
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 com.dylan.io;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* @author xusucheng
* @create 2018-01-07
**/
public class ReaderExample {
public static void main(String[] args) throws IOException{
Reader r = new FileReader("D:\\output.txt");
int data = 0;
while ((data=r.read())!=-1){
System.out.print((char)data);
}
r.close();
}
}
下一章
相關文章
- Java IO: Reader和WriterJava
- Java IO: Reader And WriterJava
- Java™ 教程(命令列I/O)Java命令列
- Java I/OJava
- 【java】I/O流Java
- Java(8)I/OJava
- Python教程:精簡概述I/O模型與I/O操作Python模型
- JAVA I/O系統Java
- Java 非同步 I/OJava非同步
- 第二十章:非同步和檔案I/O.(八)非同步
- 五種I/O模型和Java NIO原始碼分析模型Java原始碼
- Java中I/O流:阻塞和非阻塞範例Java
- 深入理解Java I/O模型Java模型
- "萬字" Java I/O 詳解Java
- I/O模型、Libuv和Eventloop模型OOP
- Java I/O系統學習系列一:File和RandomAccessFileJavarandomMac
- Java I/O模型及其底層原理Java模型
- 《Java 高階篇》六:I/O 流Java
- 計算機I/O與I/O模型計算機模型
- Java I/O系統學習系列二:輸入和輸出Java
- Java NIO學習系列五:I/O模型Java模型
- I/O流
- 【雜談】Java I/O的底層實現Java
- Java非阻塞I/O模型之NIO說明Java模型
- Netty權威指南:Java的I/O演進NettyJava
- 關於I/O
- c++ I/OC++
- “挑三揀四”地學一學Java I/OJava
- Java I/O流 複製檔案速度對比Java
- 作業系統程式、儲存和I/O作業系統
- 系統級 I/O
- Google I/O Extend 2018Go
- 網路I/O模型模型
- NodeJs 非同步 I/ONodeJS非同步
- 理解I/O Completion Port
- python 非同步 I/OPython非同步
- 02. I/O 操作
- Hadoop的I/O操作Hadoop
- Linux下的5種I/O模型與3組I/O複用Linux模型