解释
Java对对文件、管道、网络连接的中的数据的操作
以下是按照您要求的格式总结的Java各种流的信息:
文件流
字节流
FileInputStream
简介:常用的文件输入流(读)
构造方法 | 说明 |
FileInputStream(String name) | name:文件地址 内部会new File() |
FileInputStream(File file) | 无 |
FileInputStream(FileDescriptor fdObj) | 无 |
公共方法 | 说明 |
public int read() | 返回数据,只有低8位(一个字节)为有效数据,反-1为文件尾 |
int read(byte[] b) | 读取数据到b[] , 返回读取的长度,-1为文件尾 |
int read(byte[] b, int off, int len) | 从偏移量 |
byte[] readAllBytes() | 一次性将整个文件读取到内存,并返回 注意:太大抛出 OutOfMemoryError |
byte[] readNBytes(int len) | 读取len个byte内容(不足len个byte,读取真实大小的byte内容),如果没有内容可读反new byte[0] |
long transferTo(OutputStream out) | 读取数据并且写入out流,直到没有数据。 |
long skip(long n) | 跳过并丢弃输入流中的n字节数据。如果n是负数,该方法将尝试向后跳跃。返回实际跳过的字节数。 注意: 如果向前跳跃,超过当前文件的文件头,抛出IOException,向后跳跃没有影响(跳过end之后再从流中读取数据,会得到-1) |
int available() | 返回当前可读取的大小。当文件位置超出EOF时返回0。 |
void close() | 关闭流 |
final FileDescriptor getFD() | 返回FileDescriptor对象,该对象表示到这个FileInputStream正在使用的文件系统中的实际文件的连接。 |
FileChannel getChannel() | 返回与这个文件输入流关联的唯一FileChannel对象。 |
FileOutputStream
简介:常用的文件输出流
构造方法 | 说明 |
FileOutputStream(String name) | name:文件地址,内部会new File(name),默认从文件开头写入。 |
FileOutputStream(String name, boolean append) | name:文件地址,内部会new File(name),append:如果为true,则字节将写入文件的末尾而不是开头 |
FileOutputStream(File file) | 无 |
FileOutputStream(File file, boolean append) | append:如果为true,则字节将写入文件的末尾而不是开头 |
FileOutputStream(FileDescriptor fdObj) | 无 |
公共方法 | 说明 |
void write(int b) | 将b的低8位(1个字节写入流) |
void write(byte[] b) | 将b的所有类容写入 |
void write(byte[] b, int off, int len) | 将b[off]~b[off + len - 1]中的数据写入到流中。 |
void close() | 关闭流 |
final FileDescriptor getFD() | 返回FileDescriptor对象,该对象表示到这个流正在使用的文件系统中的实际文件的连接。 |
FileChannel getChannel() | 返回与这个文件输入流关联的唯一FileChannel对象。 |
字符流
FileReader
简介:FileReader 继承了InputStreamReader,请看源码,没错现在你也可以手搓源码
public class FileReader extends InputStreamReader {
public FileReader(String fileName) throws FileNotFoundException {
super(new FileInputStream(fileName));
}
public FileReader(File file) throws FileNotFoundException {
super(new FileInputStream(file));
}
public FileReader(FileDescriptor fd) {
super(new FileInputStream(fd));
}
public FileReader(String fileName, Charset charset) throws IOException {
super(new FileInputStream(fileName), charset);
}
public FileReader(File file, Charset charset) throws IOException {
super(new FileInputStream(file), charset);
}
}
FileWriter
简介:FileWrite 继承了OutputStreamWriter,请看源码,没错现在你也可以手搓源码(好熟悉,这句话是不是在那说过)。
public class FileWriter extends OutputStreamWriter {
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}
public FileWriter(String fileName, boolean append) throws IOException {
super(new FileOutputStream(fileName, append));
}
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}
public FileWriter(File file, boolean append) throws IOException {
super(new FileOutputStream(file, append));
}
public FileWriter(FileDescriptor fd) {
super(new FileOutputStream(fd));
}
public FileWriter(String fileName, Charset charset) throws IOException {
super(new FileOutputStream(fileName), charset);
}
public FileWriter(String fileName, Charset charset, boolean append) throws IOException {
super(new FileOutputStream(fileName, append), charset);
}
public FileWriter(File file, Charset charset) throws IOException {
super(new FileOutputStream(file), charset);
}
public FileWriter(File file, Charset charset, boolean append) throws IOException {
super(new FileOutputStream(file, append), charset);
}
}
数组流
简介:通常来说,针对文件的读写操作,使用文件流配合缓冲流就够用了,但为了提升效率,频繁地读写文件并不是太好,那么就出现了数组流,有时候也称为内存流。
ByteArrayInputStream
构造方法 | 说明 |
ByteArrayInputStream(byte[] buf) | buf作为它的buffer(缓冲)数组。缓冲区数组不会被复制(内部有一个this.buf引用buf)。pos默认0,count默认为数长度 |
ByteArrayInputStream(byte[] buf, int offset, int length) | 从buf[offset]开始的length个长度作为它的buffer(缓冲)数组。缓冲区数组不会被复制(内部有一个this.buf引用buf) this.pos = offset; this.count = Math.min(offset + length, buf.length); |
公共方法 | 说明 |
synchronized int read() | 返回一个int 它的低8位(一个字节)为数据位。 |
synchronized int read(byte[] b, int off, int len) | 从偏移量 |
synchronized byte[] readAllBytes() | 读取剩余的所有数据,返回读取的数据,长度的为0 表示没有数据可读 |
int readNBytes(byte[] b, int off, int len) | 同read(),只不过返回实际读取的个数,(不会反-1,最小就是 0个) |
long transferTo(OutputStream out) | 读取数据并且写入out流,直到没有数据。 |
skip | 如果n为负数不会,进行任何操作 返回实际跳过的字节数。 |
int available() | 返回当前可读取的大小。 |
boolean markSupported() | 输入流是否支持标记/重置。 |
void mark(int readAheadLimit) | 标记流中的当前位置。默认标记为开始的位置 注意:开始的位置不一定是0 比如ByteArrayInputStream(byte[] buf, int offset, int length) 如果调用的是这个构造函数,那么标记位置为offset |
synchronized void reset() | 让流回到标记处 |
void close() | 不会起任何作用 |
ByteArrayOutputStream
构造方法 | 说明 |
ByteArrayOutputStream() | 默认缓冲区大小为byte[32],不足时会自动扩容 |
ByteArrayOutputStream(int size) | 指定缓冲区大小,不足时会自动扩容 |
公共方法 | 说明 |
synchronized void write(int b) | 写入数据b的低8位(一个字节),的数据。 |
synchronized void write(byte[] b, int off, int len) | 将b[off]~b[off + len - 1]中的数据写入到流中。 |
void writeBytes(byte[] b) | 调用write(b, 0, b.length); 将b中的所有数据写入到流中去 |
void writeTo(OutputStream out) | 将ByteArrayOutputStream的全部内容写入指定的输出流参数, |
synchronized void reset() | 以覆盖形式清空缓冲区。 |
synchronized byte[] toByteArray() | 提取当前缓冲区所有有效数据 |
synchronized int size() | 返回当前缓冲区有效数据个数(字节为单位) |
synchronized String toString() | 将当前缓冲区的类容,转换为String |
synchronized String toString(String charsetName) | 通过使用命名字符集对字节进行解码,将缓冲区的内容转换为字符串。 |
synchronized String toString(Charset charset) | 通过使用指定的字符集对字节进行解码,将缓冲区的内容转换为字符串。 |
synchronized String toString(int hibyte) --已弃用 | 通过使用指定的字符集对字节进行解码,将缓冲区的内容转换为字符串。 |
void close() | 不会起任何作用 |
管道流
提示:在一个线程中同时使用这两个对象是不推荐的,因为这会导致线程死锁。
PipedInputStream
构造方法 | 说明 |
PipedInputStream() | 创建一个尚未连接的PipedInputStream,并初始化缓存(默认大小byte[1024])。 注意:在使用它之前,必须连接到一个PipedOutputStream。 |
PipedInputStream(int pipeSize) | 创建一个尚未连接的PipedInputStream并初始化缓存大小为byte[pipeSize]。 注意:在使用它之前,必须连接到一个PipedOutputStream。 |
PipedInputStream(PipedOutputStream src) | 连接到管道输出流src。并初始化缓存(默认大小byte[1024])。 |
PipedInputStream(PipedOutputStream src, int pipeSize) | 连接到管道输出流src。并初始化缓存大小为byte[pipeSize]。 |
公共方法 | 说明 |
synchronized int available() | 返回从输入流中可以不阻塞地读取的字节数 注意:输入流已经通过调用其close()方法关闭,或者管道没有连接,或者中断,则为0。 |
void close() | 关闭此输入流并释放与该流相关的任何系统资源。 |
void connect(PipedOutputStream src) | 输入流连接到管道输出流src |
synchronized int read() | 返回一个int 它的低8位(一个字节)为数据位。 这个方法会一直阻塞,直到有输入数据、检测到流的结尾或者抛出异常为止。 |
synchronized int read(byte[] b, int off, int len) | 从偏移量 这个方法会一直阻塞,直到有输入数据、检测到流的结尾或者抛出异常为止。 |
PipedOutputStream
构造方法 | 说明 |
PipedOutputStream() | 创建尚未连接到管道输入流的管道输出流。 注意:在使用它之前,接收端或发送端必须连接到管道输入流。 |
PipedOutputStream(PipedInputStream snk) | 创建连接到指定管道输入流的管道输出流。写入该流的数据字节将作为snk的输入。 |
公共方法 | 说明 |
void close() | 关闭管道输出流并释放与该流相关的系统资源。该流可能不再用于写入字节。 |
synchronized void connect(PipedInputStream snk) | 将管道输出流连接到接收器。 注意:如果此对象已经连接到其他通过管道传输的输入流,则会抛出IOException。 |
synchronized void flush() | 刷新此输出流并强制写出任何缓冲的输出字节。 注意:这将通知任何读取器有字节正在管道中等待。 |
void write(byte[] b, int off, int len) | 将指定字节数组从off开始的len字节写入此管道输出流。 注意:这个方法会阻塞,直到所有的字节都写入到输出流。 |
void write(int b) | 将指定的字节写入管道输出流。b的低8位(一个字节) |
基本数据类型流
DataInputStream
简介:继承了FilterInputStream
构造方法 | 说明 |
public DataInputStream(InputStream in) | 创建一个DataInputStream,它使用指定的底层InputStream。 |
公共方法 | 说明 |
final int read(byte[] b) | 读取数据到b[] , 返回读取的长度。流在file末尾,则返回-1; 注意:方法将阻塞,直到有输入数据、检测到文件结束或抛出异常。 |
final int read(byte[] b, int off, int len) | 从偏移量 注意:此方法将阻塞,直到有输入数据、检测到文件结束或抛出异常。 |
final boolean readBoolean() | 读取一个输入字节,如果该字节非零则返回true,如果该字节为零则返回false。 |
final byte readByte() | 读取并返回一个输入字节。该字节被视为范围为-128 ~ 127(包括-128 ~ 127)的有符号值。 |
final int readUnsignedByte() | 读取一个无符号Byte |
final char readChar() | 读一个Char(2个字节) |
final short readShort() | 无 |
final int readUnsignedShort() | 读取一个无符号Short |
final int readInt() | 无 |
final long readLong() | 无 |
final float readFloat() | 无 |
final double readDouble() | 无 |
final void readFully(byte[] b) | 从输入流中读取一些字节,并将它们存储到缓冲数组b中。读取的字节数等于b的长度。 注意:这个方法会阻塞.直到文件尾 |
final void readFully(byte[] b, int off, int len) | 读取数据到b引用的数组中下标为off ~ off + len - 1的内存中去(必须读取len个字节的数据) 注意:这个方法会阻塞.直到文件尾 |
final String readLine()---已弃用 | 从输入流中读取下一行文本。它连续读取字节,将每个字节分别转换为一个字符,直到遇到行结束符或文件末尾; |
final String readUTF() | 读取使用修改过的UTF-8格式编码的字符串。readUTF的一般约定是读取以修改后的UTF-8格式编码的Unicode字符串的表示形式;然后将这个字符串作为字符串返回。 |
static final String readUTF(DataInput in) | 从in读取使用修改过的UTF-8格式编码的字符串。readUTF的一般约定是读取以修改后的UTF-8格式编码的Unicode字符串的表示形式;然后将这个字符串作为字符串返回。 |
final int skipBytes(int n) | 跳过并丢弃输入流中的n字节数据。不支持负数。 |
DataOutputStream
简介:继承了FilterOutputStream
常用方法 | 说明 |
构造方法 | 说明 |
public DataOutputStream(OutputStream out) | 创建一个DataOutputStream,它使用指定的底层OutputStream。 |
公共方法 | 说明 |
synchronized void write(int b) | 写入单个字节的数据到输出流,其值为 |
void write(byte[] b) | 将字节数组 |
synchronized void write(byte[] b, int off, int len) | 从偏移量 |
void writeBoolean(boolean v) | 写入一个字节的数据,如果 |
void writeByte(int v) | 写入单个字节的数据到输出流,其值为 |
void writeShort(int v) | 写入两个字节的数据到输出流,其值为 |
void writeInt(int v) | 写入四个字节的数据到输出流,其值为 |
void writeLong(long v) | 写入八个字节的数据到输出流,其值为 |
void writeFloat(float v) | 写入四个字节的数据到输出流,其值为 |
void writeDouble(double v) | 写入八个字节的数据到输出流,其值为 |
void writeBytes(String s) | 将字符串 |
void writeChars(String s) | 将字符串 |
void writeUTF(String str) | 将字符串 |
void flush() | 刷新此数据输出流,强制写出所有缓冲的输出字节。 |
void close() | 关闭此数据输出流,并释放与此流相关联的所有系统资源。 |