Java IO
Java IO
- 1、輸入流
- 2、輸出流
- 輸入流
Java程序可以從其中讀入一個字節序列的對象成爲輸入流,輸入流包含對字節和字符操作流,InputStream和Reader。
1.1. InputStream
InputStream是所有輸入流字節流的父類,且此類是抽象類
1.1.1. InputStream源代碼中幾個重要的方法
**package java.io;
public abstract class InputStream implements Closeable {
/**
* 讀取一個字節
*/
public abstract int read() throws IOException;
/**
* 讀取一個字節數組
*/
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
/**
* 讀取一個字節數組
*/
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
/**
* 關閉流
*/
public void close() throws IOException {}
}**
... prompt'''
1.2 Reader
Reader是讀字符流的抽象類
1.2.1 Reader源代碼中幾個重要的方法
package java.io;
public abstract class Reader implements Readable, Closeable {
/**
* 讀取一個字符
*/
public int read() throws IOException {
char cb[] = new char[1];
if (read(cb, 0, 1) == -1)
return -1;
else
return cb[0];
}
/**
* 讀取一個字符數組
*/
public int read(char cbuf[]) throws IOException {
return read(cbuf, 0, cbuf.length);
}
/**
* 讀取一個字符數組
*/
abstract public int read(char cbuf[], int off, int len) throws IOException;
/**
* 關閉流
*/
abstract public void close() throws IOException;
}
... prompt'''
2 輸出流
Java程序可以向其中寫入一個字節序列的對象成爲輸出流,輸出流包含對字節和字符操作流,OutputStream和Writer。
2.1. OutputStream
OutputStream是所有輸出流字節流的父類,且此類是抽象類
2.1.1 OutputStream源代碼中幾個重要的方法
**package java.io;
public abstract class OutputStream implements Closeable, Flushable {
/**
* 輸出一個字節
*/
public abstract void write(int b) throws IOException;
/**
* 輸出一個字節數組
*/
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
/**
* 輸出一個字節數組或部分內容
*/
public void write(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
/**
* 刷新流
*/
public void flush() throws IOException {
}
/**
* 關閉流
*/
public void close() throws IOException {
}
}**
... prompt'''
2.2 Writer
Reader是讀字符流的抽象類
2.2.1 Writer 源代碼中幾個重要的方法
package java.io;
public abstract class Writer implements Appendable, Closeable, Flushable {
public void write(int c) throws IOException {
synchronized (lock) {
if (writeBuffer == null){
writeBuffer = new char[writeBufferSize];
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
}
public void write(char cbuf[]) throws IOException {
write(cbuf, 0, cbuf.length);
}
abstract public void write(char cbuf[], int off, int len) throws IOException;
public void write(String str) throws IOException {
write(str, 0, str.length());
}
public void write(String str, int off, int len) throws IOException {
synchronized (lock) {
char cbuf[];
if (len <= writeBufferSize) {
if (writeBuffer == null) {
writeBuffer = new char[writeBufferSize];
}
cbuf = writeBuffer;
} else { // Don't permanently allocate very large buffers.
cbuf = new char[len];
}
str.getChars(off, (off + len), cbuf, 0);
write(cbuf, 0, len);
}
}
public Writer append(CharSequence csq) throws IOException {
if (csq == null)
write("null");
else
write(csq.toString());
return this;
}
public Writer append(CharSequence csq, int start, int end) throws IOException {
CharSequence cs = (csq == null ? "null" : csq);
write(cs.subSequence(start, end).toString());
return this;
}
public Writer append(char c) throws IOException {
write(c);
return this;
}
abstract public void flush() throws IOException;
abstract public void close() throws IOException;
}
... prompt'''