Java IO:FileInputStream和FileOutputStream使用詳解及原始碼分析
阿新 • • 發佈:2019-02-20
1 使用方法
FileInputStream即檔案輸入流,使用它從檔案中獲得位元組流,FileOutputStream即問價輸出流,使用它將位元組流寫入檔案。
1.1 方法介紹
FileInputStream提供的API如下:
FileInputStream(File file) // 建立“File物件”對應的“檔案輸入流”
FileInputStream(FileDescriptor fd) // 建立“檔案描述符”對應的“檔案輸入流”
FileInputStream(String path) // 建立“檔案(路徑為path)”對應的“檔案輸入流”
int available() // 返回“剩餘的可讀取的位元組數”或者“skip的位元組數”
void close() // 關閉“檔案輸入流”
FileChannel getChannel() // 返回“FileChannel”
final FileDescriptor getFD() // 返回“檔案描述符”
int read() // 返回“檔案輸入流”的下一個位元組
int read(byte[] buffer, int off, int len) // 讀取“檔案輸入流”的資料並存在到buffer,從off開始儲存,儲存長度是len。
long skip(long n) // 跳過n個位元組
FileOutputStream提供的API如下:
FileOutputStream(File file) // 建立“File物件”對應的“檔案輸入流”;預設“追加模式”是false,即“寫到輸出的流內容”不是以追加的方式新增到檔案中。
FileOutputStream(File file, boolean append) // 建立“File物件”對應的“檔案輸入流”;指定“追加模式”。
FileOutputStream(FileDescriptor fd) // 建立“檔案描述符”對應的“檔案輸入流”;預設“追加模式”是false,即“寫到輸出的流內容”不是以追加的方式新增到檔案中。
FileOutputStream(String path) // 建立“檔案(路徑為path)”對應的“檔案輸入流”;預設“追加模式”是false,即“寫到輸出的流內容”不是以追加的方式新增到檔案中。
FileOutputStream(String path, boolean append) // 建立“檔案(路徑為path)”對應的“檔案輸入流”;指定“追加模式”。
void close() // 關閉“輸出流”
FileChannel getChannel() // 返回“FileChannel”
final FileDescriptor getFD() // 返回“檔案描述符”
void write(byte[] buffer, int off, int len) // 將buffer寫入到“檔案輸出流”中,從buffer的off開始寫,寫入長度是len。
void write(int n) // 寫入位元組n到“檔案輸出流”中
1.2 使用示例
/**
* 在原始碼所在目錄生成一個test.txt,並寫入abcdefghijklmn123456
*/
public void testFileOutputStream() {
try {
byte [] content = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'};
//床架test.txt檔案
File file = new File("test.txt");
//建立檔案輸出流
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(content, 0, 14);
//PrintStream寫入方便
PrintStream printStream = new PrintStream(outputStream);
printStream.print("123456");
printStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 測試檔案輸入流
*/
public void testFileInputStream() {
try {
//新建輸入流,檔案中的內容為abcdefghijklmn123456
FileInputStream inputStream = new FileInputStream("test.txt");
//讀取一個位元組a
System.out.println("讀取一個位元組: " + inputStream.read());
//跳過兩個位元組 b c
inputStream.skip(2);
//讀取三個位元組到buff中def
byte [] buff = new byte[3];
inputStream.read(buff, 0, 3);
System.out.println("buff中的內容為: " + new String(buff));
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
執行結果如下:
讀取一個位元組: 97
buff中的內容為: def
2 原始碼分析
2.1FileInputStream原始碼分析
2.1.1 構造方法
FileInputStream提供三個構造方法,區別是傳入的引數型別(檔案路徑,FIle物件,檔案描述符物件)。
/**
* Creates a <code>FileInputStream</code> by
* opening a connection to an actual file,
* the file named by the path name <code>name</code>
* in the file system. A new <code>FileDescriptor</code>
* object is created to represent this file
* connection.
* <p>
* First, if there is a security
* manager, its <code>checkRead</code> method
* is called with the <code>name</code> argument
* as its argument.
* <p>
* If the named file does not exist, is a directory rather than a regular
* file, or for some other reason cannot be opened for reading then a
* <code>FileNotFoundException</code> is thrown.
*
* @param name the system-dependent file name.
* @exception FileNotFoundException if the file does not exist,
* is a directory rather than a regular file,
* or for some other reason cannot be opened for
* reading.
* @exception SecurityException if a security manager exists and its
* <code>checkRead</code> method denies read access
* to the file.
* @see java.lang.SecurityManager#checkRead(java.lang.String)
*/
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
/**
* Creates a <code>FileInputStream</code> by
* opening a connection to an actual file,
* the file named by the <code>File</code>
* object <code>file</code> in the file system.
* A new <code>FileDescriptor</code> object
* is created to represent this file connection.
* <p>
* First, if there is a security manager,
* its <code>checkRead</code> method is called
* with the path represented by the <code>file</code>
* argument as its argument.
* <p>
* argument as its argument.
* <p>
* If the named file does not exist, is a directory rather than a regular
* file, or for some other reason cannot be opened for reading then a
* <code>FileNotFoundException</code> is thrown.
*
* @param file the file to be opened for reading.
* @exception FileNotFoundException if the file does not exist,
* is a directory rather than a regular file,
* or for some other reason cannot be opened for
* reading.
* @exception SecurityException if a security manager exists and its
* <code>checkRead</code> method denies read access to the file.
* @see java.io.File#getPath()
* @see java.lang.SecurityManager#checkRead(java.lang.String)
*/
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);
}
/**
* Creates a <code>FileInputStream</code> by using the file descriptor
* <code>fdObj</code>, which represents an existing connection to an
* actual file in the file system.
* <p>
* If there is a security manager, its <code>checkRead</code> method is
* called with the file descriptor <code>fdObj</code> as its argument to
* see if it's ok to read the file descriptor. If read access is denied
* to the file descriptor a <code>SecurityException</code> is thrown.
* <p>
* If <code>fdObj</code> is null then a <code>NullPointerException</code>
* is thrown.
* <p>
* This constructor does not throw an exception if <code>fdObj</code>
* is {@link java.io.FileDescriptor#valid() invalid}.
* However, if the methods are invoked on the resulting stream to attempt
* I/O on the stream, an <code>IOException</code> is thrown.
*
* @param fdObj the file descriptor to be opened for reading.
* @throws SecurityException if a security manager exists and its
* <code>checkRead</code> method denies read access to the
* file descriptor.
* @see SecurityManager#checkRead(java.io.FileDescriptor)
*/
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.
*/
fd.attach(this);
}
2.2 FileOutputStream原始碼分析
2.1.1 構造方法
public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false);
}
/**
* Creates a file output stream to write to the file with the specified
* name. If the second argument is <code>true</code>, then
* bytes will be written to the end of the file rather than the beginning.
* A new <code>FileDescriptor</code> object is created to represent this
* file connection.
* <p>
* First, if there is a security manager, its <code>checkWrite</code>
* method is called with <code>name</code> as its argument.
* <p>
* If the file exists but is a directory rather than a regular file, does
* not exist but cannot be created, or cannot be opened for any other
* reason then a <code>FileNotFoundException</code> is thrown.
*
* @param name the system-dependent file name
* @param append if <code>true</code>, then bytes will be written
* to the end of the file rather than the beginning
* @exception FileNotFoundException if the file exists but is a directory
* rather than a regular file, does not exist but cannot
* be created, or cannot be opened for any other reason.
* @exception SecurityException if a security manager exists and its
* <code>checkWrite</code> method denies write access
* to the file.
* @see java.lang.SecurityManager#checkWrite(java.lang.String)
* @since JDK1.1
*/
public FileOutputStream(String name, boolean append)
throws FileNotFoundException
{
this(name != null ? new File(name) : null, append);
}
public FileOutputStream(File file) throws FileNotFoundException {
this(file, false);
}
/**
* Creates a file output stream to write to the file represented by
* the specified <code>File</code> object. If the second argument is
* <code>true</code>, then bytes will be written to the end of the file
* rather than the beginning. A new <code>FileDescriptor</code> object is
* created to represent this file connection.
* <p>
* First, if there is a security manager, its <code>checkWrite</code>
* method is called with the path represented by the <code>file</code>
* argument as its argument.
* <p>
* If the file exists but is a directory rather than a regular file, does
* not exist but cannot be created, or cannot be opened for any other
* reason then a <code>FileNotFoundException</code> is thrown.
*
* @param file the file to be opened for writing.
* @param append if <code>true</code>, then bytes will be written
* to the end of the file rather than the beginning
* @exception FileNotFoundException if the file exists but is a directory
* rather than a regular file, does not exist but cannot
* be created, or cannot be opened for any other reason
* @exception SecurityException if a security manager exists and its
* be created, or cannot be opened for any other reason
* @exception SecurityException if a security manager exists and its
* <code>checkWrite</code> method denies write access
* to the file.
* @see java.io.File#getPath()
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkWrite(java.lang.String)
* @since 1.4
*/
public FileOutputStream(File file, boolean append)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(name);
}
if (name == null) {
throw new NullPointerException();
}
if (file.isInvalid()) {
throw new FileNotFoundException("Invalid file path");
}
this.fd = new FileDescriptor();
fd.attach(this);
this.append = append;
this.path = name;
open(name, append);
}
/**
* Creates a file output stream to write to the specified file
* descriptor, which represents an existing connection to an actual
* file in the file system.
* <p>
* First, if there is a security manager, its <code>checkWrite</code>
* method is called with the file descriptor <code>fdObj</code>
* argument as its argument.
* <p>
* If <code>fdObj</code> is null then a <code>NullPointerException</code>
* is thrown.
* <p>
* This constructor does not throw an exception if <code>fdObj</code>
* is {@link java.io.FileDescriptor#valid() invalid}.
* However, if the methods are invoked on the resulting stream to attempt
* I/O on the stream, an <code>IOException</code> is thrown.
*
* @param fdObj the file descriptor to be opened for writing
* @exception SecurityException if a security manager exists and its
* <code>checkWrite</code> method denies
* write access to the file descriptor
* @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
*/
public FileOutputStream(FileDescriptor fdObj) {
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkWrite(fdObj);
}
this.fd = fdObj;
this.append = false;
this.path = null;
fd.attach(this);
}