前言
月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂)
央是一片海洋,海乃百川,代表着一块海绵(吸纳万物)
泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出)
月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容
希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄!
一.FileInputStream的作用:
直接看源码注释(我的翻译可能不太准,如果道友们有更棒的理解,可以留言或者私信)
/**
* A <code>FileInputStream</code> obtains input bytes
* from a file in a file system. What files
* are available depends on the host environment.
* 1.FileInputStream从文件系统中的文件中获取输入字节。哪些文件可用取决于主机环境。
* <p><code>FileInputStream</code> is meant for reading streams of raw bytes
* such as image data. For reading streams of characters, consider using
* <code>FileReader</code>.
* 2.FileInputStream用于读取原始字节流,例如图像数据。要读取字符流,请考虑使用FileReader
* @author Arthur van Hoff
* @see java.io.File
* @see java.io.FileDescriptor
* @see java.io.FileOutputStream
* @see java.nio.file.Files#newInputStream
* @since JDK1.0
*/
二.类图:
三.成员变量:
/* File Descriptor - handle to the open file */
//文件描述符 - 打开文件的句柄
private final FileDescriptor fd;
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
* 引用文件的路径(如果流是用文件描述符创建的,则为 null)
*/
private final String path;
private FileChannel channel = null;
private final Object closeLock = new Object();
private volatile boolean closed = false;
四.构造方法:
/**
* 1.通过打开与实际文件的连接来创建FileInputStream,
* 该文件由文件系统中的路径名name命名。创建一个新的FileDescriptor对象来表示此文件连接
* 2.首先,如果有一个安全管理器,它的checkRead方法被调用,并使用name参数作为它的参数
* 3.如果指定的文件不存在,是一个目录而不是常规文件,或者由于某些其他原因无法打开读取,
* 则抛出FileNotFoundException
*/
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
/**
* 1.通过打开与实际文件的连接来创建FileInputStream,
* 该文件由文件系统中的File对象file命名。创建一个新的FileDescriptor对象来表示此文件连接
* 2.首先,如果有一个安全管理器,它的checkRead方法被调用,
* 以file参数表示的路径作为它的参数
* 3.如果指定的文件不存在,是一个目录而不是常规文件,或者由于某些其他原因无法打开读取,
* 则抛出FileNotFoundException
*/
public FileInputStream(File file) throws FileNotFoundException {
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
fd = new FileDescriptor();
fd.attach(this);
path = name;
open(name);
}
/**
* 1.通过使用文件描述符fdObj创建一个FileInputStream,它表示到文件系统中实际文件的现有连接
* 2.如果有一个安全管理器,它的checkRead方法被调用,
* 文件描述符 fdObj作为它的参数,看看是否可以读取文件描述符。
* 如果对文件描述符的读取访问被拒绝,则抛出SecurityException
* 3.如果fdObj为 null,则抛出NullPointerException
* 4.如果fdObj是java.io.FileDescriptor.valid() invalid,这个构造函数不会抛出异常。
* 但是,如果在结果流上调用这些方法以尝试对流进行 IO,则会引发IOException。
*/
public FileInputStream(FileDescriptor fdObj) {
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkRead(fdObj);
}
fd = fdObj;
path = null;
/*
* FileDescriptor is being shared by streams.
* Register this stream with FileDescriptor tracker.
* FileDescriptor 正在由流共享。使用 FileDescriptor 跟踪器注册此流。
*/
fd.attach(this);
}
五.内部方法:
read
/**
* 从此输入流中读取一个字节的数据。如果尚无可用输入,则此方法会阻塞
*/
public int read() throws IOException {
return read0();
}
/**
* 从此输入流中读取最多b.length个字节的数据到一个字节数组中。此方法会阻塞,直到某些输入可用
*/
public int read(byte b[]) throws IOException {
return readBytes(b, 0, b.length);
}
/**
* 从此输入流中读取最多len个字节的数据到一个字节数组中。如果len不为零,则该方法会阻塞,
* 直到某些输入可用;否则,不读取任何字节并返回0
*/
public int read(byte b[], int off, int len) throws IOException {
return readBytes(b, off, len);
}
close
/**
* 1.关闭此文件输入流并释放与该流关联的所有系统资源
* 2.如果此流具有关联的通道,则该通道也将关闭。
*/
public void close() throws IOException {
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}
if (channel != null) {
channel.close();
}
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
}
});
}
getFD
/**
* 返回FileDescriptor对象,该对象表示与此FileInputStream使用的文件系统中实际文件的连接。
*/
public final FileDescriptor getFD() throws IOException {
if (fd != null) {
return fd;
}
throw new IOException();
}
getChannel
/**
* 1.返回与此文件输入流关联的唯一java.nio.channels.FileChannel对象
* 2.返回通道的初始 java.nio.channels.FileChannelposition()
* 将等于到目前为止从文件中读取的字节数。从此流中读取字节将增加通道的位置。
* 显式或通过读取更改通道的位置将更改此流的文件位置
*/
public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, path, true, false, this);
}
return channel;
}
}
六.总结:
基础的文件操作流,好像也没什么说的...