Java IO: Pipes
- 管道支持線程間通訊粱哼,但不是線程安全的傀蚌。
Java IO: Byte & Char Arrays
- ByteArrayInputStream 或 CharArrayReader 作用讀取字節(jié)數(shù)組或字符數(shù)組能庆。
- ByteArrayOutputStream 和 CharArrayWriter 同理
Java IO: System.in, System.out, and System.error
- 可以通過(guò)System.setIn(), System.setOut()砖瞧, System.setErr() 重定向系統(tǒng)默認(rèn)輸入和輸出屯蹦。
Java IO: Streams
- stream和數(shù)組不一樣嘀趟,是不支持定位移動(dòng)的脐区。
- inputStream 的 read() 返回一個(gè)字節(jié),如果沒(méi)有可讀字節(jié)就返回-1她按。
- 因?yàn)镕ileInputStream每次返回一個(gè)字節(jié)效率太低牛隅,通過(guò)BufferedInputStream的組合炕柔,一次可以返回一部分內(nèi)
BufferedOutputStream 同理。InputStream input = new BufferedInputStream(new FileInputStream("c:\\data\\input-file.txt"));
Java IO: Readers and Writers
- 通過(guò)組合方式倔叼,將字節(jié)流轉(zhuǎn)換為字符流:
Writer同理汗唱。Reader reader = new InputStreamReader(inputStream);
- Buffer的組合也是類似的
Reader reader = new BufferedReader(new FileReader(...)); Writer writer = new BufferedWriter(new FileWriter(...));
Java IO Exception Handling From Java 7
- Java7支持發(fā)生異常也能關(guān)閉資源:
try ( java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName); java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset) ) { // 處理流 }
Java IO: InputStream
read(byte[] array) 一次獲取多個(gè)字節(jié),比read()效率高丈攒。
OutputStream同理哩罪。
read(byte[])方法會(huì)嘗試讀取與給定字節(jié)數(shù)組容量一樣大的字節(jié)數(shù),返回值說(shuō)明了已經(jīng)讀取過(guò)的字節(jié)數(shù)巡验。如果InputStream內(nèi)可讀的數(shù)據(jù)不足以填滿字節(jié)數(shù)組际插,那么數(shù)組剩余的部分將包含本次讀取之前的數(shù)據(jù)。記得檢查有多少數(shù)據(jù)實(shí)際被寫入到了字節(jié)數(shù)組中显设。如果inputStream的子類支持mark()和reset()方法框弛,它會(huì)重載markSupported(),返回true捕捂。
mark建立書簽瑟枫,reset返回到書簽的位置。
Java IO: FileOutputStream
- 根據(jù)構(gòu)造函數(shù)的參數(shù)指攒,決定是覆蓋文件內(nèi)容還是追加文件內(nèi)容:
OutputStream output = new FileOutputStream("c:\\data\\output-text.txt", true); //appends to file OutputStream output = new FileOutputStream("c:\\data\\output-text.txt", false); //overwrites file
Java IO: RandomAccessFile
- 創(chuàng)建實(shí)例對(duì)象:
RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");
file.seek(200); // 移動(dòng)位置
long pointer = file.getFilePointer(); // 獲取當(dāng)前位置
int aByte = file.read(); // 讀取當(dāng)前位置的字節(jié)慷妙,并移動(dòng)到下一個(gè)位置
file.close();
```
# Java IO: File
- 創(chuàng)建目錄:
```java
File file = new File("c:\\data\\input-file.txt");
boolean dirCreated = file.mkdirs();
```
- 獲取目錄下文件列表:
```java
File file = new File("c:\\data");
String[] fileNames = file.list();
File[] files = file.listFiles();
```
# Java IO: BufferedInputStream
- BufferedInputStream的優(yōu)化:http://www.importnew.com/9363.html
- 支持 mark() 和 reset()
# Java IO: PushbackInputStream
```java
PushbackInputStream input = new PushbackInputStream(new FileInputStream("c:\\data\\input.txt"));
int data = input.read();
input.unread(data); // 往回倒退
```
# Java IO: SequenceInputStream
- SequenceInputStream按順序讀取多個(gè)inputStream:
![SequenceInputStream](http://upload-images.jianshu.io/upload_images/6160287-d2eea6d88c3207dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
```java
InputStream input1 = new FileInputStream("c:\\data\\file1.txt");
InputStream input2 = new FileInputStream("c:\\data\\file2.txt");
SequenceInputStream sequenceInputStream =
new SequenceInputStream(input1, input2);
int data = sequenceInputStream.read();
while(data != -1){
System.out.println(data);
data = sequenceInputStream.read();
}
Java IO: DataInputStream
-
作用是把字節(jié)類型轉(zhuǎn)換成Java其他的原生類型(因?yàn)樵愋偷拇笮《即笥谧止?jié)單位大小,按原生類型單位讀寫的場(chǎng)景很方便):
Java IO: PrintStream
- 打印輸出原生類型:
PrintStream printStream = new PrintStream(outputStream); // 組合方式使用
printStream.print(true);
printStream.print((int) 123);
printStream.print((float) 123.456);
printStream.close();
- System.out and System.err are PrintStreams
Java IO: ObjectInputStream
- 按對(duì)象單位讀寫允悦,前提是對(duì)象類實(shí)現(xiàn)了Serializable接口:
import java.io.*;
public class ObjectInputStreamExample {
public static class Person implements Serializable {
public String name = null;
public int age = 0;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream objectOutputStream =
new ObjectOutputStream(new FileOutputStream("data/person.bin"));
Person person = new Person();
person.name = "Jakob Jenkov";
person.age = 40;
objectOutputStream.writeObject(person);
objectOutputStream.close();
ObjectInputStream objectInputStream =
new ObjectInputStream(new FileInputStream("data/person.bin"));
Person personRead = (Person) objectInputStream.readObject();
objectInputStream.close();
System.out.println(personRead.name);
System.out.println(personRead.age);
}
}
Java IO: Serializable
- 現(xiàn)在更推薦序列化成JSON等其他可讀的格式膝擂,實(shí)現(xiàn)Serializable接口不是很好。
Java IO: Reader
- 以字符為單位讀寫隙弛,拿UTF-8作為例子架馋,一個(gè)漢字字符有3個(gè)字節(jié)長(zhǎng)度。
Java IO: InputStreamReader
- 轉(zhuǎn)換字節(jié)流到字符流全闷。
- 構(gòu)造方法支持指定字符編碼叉寂。
Java IO: StreamTokenizer
- 將字符流按token拆分:
StreamTokenizer streamTokenizer = new StreamTokenizer(
new StringReader("Mary had 1 little lamb..."));
while(streamTokenizer.nextToken() != StreamTokenizer.TT_EOF){ // 判斷字符流是否結(jié)束
if(streamTokenizer.ttype == StreamTokenizer.TT_WORD) { // 如果拆分后的token是字符
System.out.println(streamTokenizer.sval);
} else if(streamTokenizer.ttype == StreamTokenizer.TT_NUMBER) { // 如果token是數(shù)字
System.out.println(streamTokenizer.nval);
} else if(streamTokenizer.ttype == StreamTokenizer.TT_EOL) { // 如果token是換行
System.out.println();
}
}
streamTokenizer.close();