JDK 1.8 API閱讀與翻譯(2) FileInputStream

JDK 1.8 API閱讀與翻譯(2) FileInputStream

Author: Alex Wang

Date: Feb 28 2019

原文:

public class FileInputStream extends InputStream

A FileInputStream obtains input bytes from a file in a file system. What files are availiable depends on the host environment.

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

翻譯:
公共類FileInputStream繼承InputStream類群凶。一個FileInputStream類包含來自一個文件的輸入字節(jié)敬锐。可用的文件取決于主機的環(huán)境鼓择。FileInputStream意味著讀取輸入的生子節(jié)像是讀取圖片數(shù)據(jù)什么的。對于讀取字符流蜗元,可以考慮用FileReader類鹏溯。

Constructor Summary

Constructors

FileInputStream(File file)

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

FileInputStream(FileDescriptor fdObj)

Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.

FileInputStream(String name)

Creates a FileInputStream by opening a connection to a actual file, the file named by the path name name in the file system,.

構(gòu)造器總結(jié)

FileInputStream(File file)

通過打開和實際文件的鏈接創(chuàng)建一個FileInputStream,這個文件由文件系統(tǒng)中的File對象命名太抓。

FileInputStream(FileDescriptor fdObj)

通過描述符fdObj來創(chuàng)建一個FileInputStream空闲,這個描述符代表一個現(xiàn)成的鏈接文件系統(tǒng)中文件的鏈接。

FileInputStream(String name)

通過打開和世界文件的鏈接創(chuàng)建一個FileInputStream走敌,這個文件由文件系統(tǒng)中的路徑名來命名碴倾。


Method Summary

Modifier and Type Method and Description
int available() Returns an estimate of the number of remaining bytes that can be read(or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
void close() Closes this file input stream and releases any system resources associated with the stream.
protected void finalized() Ensures that the close method of this file input stream is called when there are no more references to it.
FileChannel getChannel() Returns the unique FileChannel object associated with this file input stream
FileDescriptor getFD() Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream
int read() Reads a byte of data from this input stream
int read(byte[] b) Reads up to b.length bytes from this input stream into an array of bytes
int read(byte[] b, int off, int len) Reads up to len bytes of data from this input stream into an array of bytes.
long skip(long n) Skips over and discards n bytes of data from the input stream
方法類型 方法及其描述
int available() 從這個輸入流中返回一個可讀的字節(jié)數(shù)的估計值(或者可被跳過的),而不會阻塞下一個調(diào)用這個輸入流的方法掉丽。
void close() 關(guān)閉這個輸入流并且釋放任何相關(guān)的系統(tǒng)資源
protected void finalized() 當(dāng)這個不會再被引用的時候確保close方法被調(diào)用
FileChannel getChannel() 返回一個唯一的和這個輸入流相關(guān)的FileChannel對象
FileDescriptor getFD() 返回描述符跌榔,用于描述被這個輸入流使用的文件系統(tǒng)中的文件和實際文件的鏈接
int read() 讀取這個輸入流中一個字節(jié)的數(shù)據(jù)
int read(byte[] b) 在這個輸入流讀到長度為b字節(jié)的數(shù)據(jù),并寫入緩存數(shù)組byte[]中
int read(byte[] b, int off, int len) 從這個輸入流中讀取長度為len字節(jié)的數(shù)據(jù)捶障,并把它寫入緩存數(shù)組b中
long skip(long n) 在這個流中跳過n個字節(jié)的數(shù)據(jù)

Constructor Detail

FileInputStream

public FileInputStream(String name) throws FileNotFoundException

Creates a FileInputStream by opening a connection to an actual file, the file named buy the path name name in the file system. A new FileDescriptor object is created to represent this file connection.

First, if there is a security manager, its checkRead method is called with the name argument as its arguments.

If the named file does not exist, is a directory rather than a regualr file, or for some other reason cannot be open for reading then a FIleNotFoundException is thrown.

Parameters:

name - the system-dependent file name.

Throws:

FileNoeFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be open for reading .

SecurityException - if a security manager exists and its checkRead method denies read access to the file.

See Also:

SecurityManager. checkRead(Java.lang.String)


FileInputStream

通過打開和實際文件的鏈接創(chuàng)建一個FileInputStream僧须,這個文件由文件系統(tǒng)中的File對象命名。一個新的描述符被創(chuàng)建用來代表這個鏈接项炼。

首先担平,如果這有個安全管理器,則它的checkRead方法會被調(diào)用锭部,并把name參數(shù)當(dāng)作它自己的參數(shù)[沒理解這個地方??]

如果叫這個名字的文件不存在驱闷,這是個目錄而不是個普通文件,或者因為其他原因讀不了這個文件空免,方法會拋出一個FileNotFoundException異常空另。

參數(shù):

name - 系統(tǒng)里面這個文件的名字

拋出:

FileNotFoundException -如果文件不存在,或者輸入的文件名是個目錄蹋砚、不是普通文件扼菠,亦或者因為其他原因讀不了這個東西,那么會拋出這個異常坝咐。

SecurityException -如果一個安全管理器存在循榆,然后它的checkRead方法否決了對這個文件的讀取。


FileInputStream

public FileInputStream(File file) throws FileNotFoundException

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. A new FileDescriptor object created to represent this file connection.

First, if there is a security manager, its checkRead method is called with the path reapreseneted by the file argument as its argument.

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 FileNotFoundException is thrown.

Parameters:

file - the file to be opened for reading.

Throws:

FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reson cannot be openedf for reading.

SecurityExceptionb - if a security manager exists and its checkRead method denies read access to the file.

See Also:

File.getPath(), SecurityManager.checkRead(java.lang.String)


FileInputStream

通過打開和一個實際文件的鏈接來創(chuàng)建一個輸入流墨坚,這個文件以文件系統(tǒng)中File Object的名字來命名秧饮。一個新的文件描述符會被創(chuàng)建去代表這個鏈接。

首先,這會有一個安全管理器盗尸,它的checkRead方法會被調(diào)用柑船,并且把path這個參數(shù)當(dāng)作自己的參數(shù)。

如果這個文件不存泼各,或者這個輸入的參數(shù)是一個目錄而不是一個普通文件鞍时,亦或是由于其他原因這個文件打不開,該方法都會拋出一個FileNotFound的異常扣蜻。

參數(shù):

file - 要讀取的文件

拋出:

FileNotFoundException -如果文件不存在逆巍,或者輸入的文件名是個目錄、不是普通文件莽使,亦或者因為其他原因讀不了這個東西锐极,那么會拋出這個異常。

SecurityException -如果一個安全管理器存在芳肌,然后它的checkRead方法否決了對這個文件的讀取灵再。


FileInputStream

public FileInputStream(FileDescriptor fdObj)

Create a FileInputStream by using the file descriptor dfObj , which represents an existing connection to an actual file int he file system.

If there is a security manager, its checkRead method is called with the file descriptor fdObj as its arguments to see if it's ok to read the file descriptor. If read accessis denied to the file descriptor a SecurityException is thrown.

If fdObj is null then a NullPointerException is thrown.

This constructor does not throw an exception if fdObj is invalid. However, if the methods are invoked on the resulting stream to attempt I/O on the stream an IOException is thrown.

Parameters:
fdObj - the file descriptor to be opened for reading.

Throws:
SecurityException - if a sevurity manager exists and its checkRead method denies read access to the file descriptor.

See Also:
SecurityManager.checkRead(java.io.File.Descriptor)


FileInputStream

通過打開和一個實際文件的鏈接來創(chuàng)建一個輸入流,這個文件以文件系統(tǒng)中File Object的名字來命名庇勃。一個新的文件描述符會被創(chuàng)建去代表這個鏈接檬嘀。

如果有安全管理器,則使用文件描述符fdObj作為參數(shù)調(diào)用其checkRead方法责嚷,來檢查是否可以讀取文件描述符鸳兽。如果對文件描述符拒絕讀取訪問,則拋出SecurityException罕拂。

如果fdObj是空的揍异,那么該方法會拋出一個空指針異常。

如果fbObj是空的爆班,那么構(gòu)造器不會拋出異常衷掷。然而,如果這個結(jié)果流上調(diào)用方法以嘗試對流進行I/O操作柿菩,則該方法會拋出IOException戚嗅。

參數(shù):

fdObj - 要被讀取或者打開的文件描述符

拋出:

SecurityException -如果一個安全管理器存在,然后它的checkRead方法否決了對這個文件的讀取枢舶。


Method Detail

read

public int read() throws IOException

Reads a byte of data from this input stream. This method blocks if no input is yet available.

Specified by:

read in class InputStream

Returns:

the next byte of data, or -1 if the end of the file is reached.

Throws:

IOException - if an I/O error occurs.


read

從輸入流中讀取一個字節(jié)的數(shù)據(jù)懦胞。這個方法阻塞直到?jīng)]有東西可以讀取。

具體說明:

在InputStream類中的read方法

返回:

讀取下一個字節(jié)中的數(shù)據(jù)凉泄,或者當(dāng)讀到文件結(jié)尾的時候返回-1

拋出:

IOException - 當(dāng)I/O錯誤發(fā)生時躏尉。


read

public int read(byte[] b) throws IOException

Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Overrides:

read in class InputStream

Parameters:

b - the buffer into which the data is read.

Returns:

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

Throws:

IOException - if an I/O error occurs.

See Also:

InputStream.read(bytep[], int, int)


read:

將流中長度為b.length的數(shù)據(jù)讀入字節(jié)數(shù)組b中。這個方法阻塞直到有數(shù)據(jù)可讀后众。

重寫:

這個方法時InputStream類中read方法的重寫胀糜。

參數(shù):

b - 讀入數(shù)據(jù)的緩存數(shù)組

返回:

返回讀入緩存數(shù)組中的字節(jié)數(shù)颅拦,或者讀到文件結(jié)尾的時候返回一個-1。

拋出:

IOException - if an I/O error occurs.


read

public int read(byte[] b, int off, int len) throws IOException

Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

Overrides:

read in class InputStream.

Parameters:

b - the buffer into which the data is read.

off - the start offset in the destination array b.

len - the maximum number of byters read.

Returns:

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.

Throws:

NullPointerException - If b is null.

IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off.

IOException - if an I/O error occurs.

See Also:
InputStream.read()


Read:

從輸入流中讀取長度為len字節(jié)的數(shù)據(jù)寫入數(shù)組b中教藻。如果len不是0距帅,那么這個方法阻塞直到輸入流有數(shù)據(jù)可讀;或者怖竭,沒數(shù)據(jù)讀的時候這個方法返回0.

重寫:

這個方法是Input Stream類中read方法的重寫锥债。

參數(shù):

b - 存儲讀取數(shù)據(jù)的緩存陡蝇。

off - 寫入數(shù)據(jù)到數(shù)組b時的偏移量痊臭。

len - 可讀取字節(jié)數(shù)的最大值。

返回:

返回讀入數(shù)組中數(shù)據(jù)的字節(jié)數(shù)登夫,或者在讀到文件結(jié)尾的時候返回-1.

拋出:

NullPointerException - 如果數(shù)組b是null广匙。

IndexOutOfBoundsException - 當(dāng)off是負數(shù),或者len是負數(shù)恼策,或者len比b.length - off大的時候

IOException - 當(dāng)一個I/O錯誤發(fā)生的時候


Skip

public long skip(long n) throws IOException

Skips over and discards n bytes of data from the input stream.
The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. If n is negative, the method will try to skip backwards. In case the backing file does not support backward skip at its current position, an IOException is thrown. The actual number of bytes skipped is returned. If it skips forwards, it returns a positive value. If it skips backwards, it returns a negative value.

This method may skip more bytes than what are remaining in the backing file. This produces no exception and the number of bytes skipped may include some number of bytes that were beyond the EOF of the backing file. Attempting to read from the stream after skipping past the end will result in -1 indicating the end of the file.

Overrides:

skip in class InputStream

Parameters:

n - the number of bytes to be skipped.

Returns:

the actual number of bytes skipped.

Throws:

IOException - if n is negative, if the stream does not support seek, or if an I/O error occurs.


Skip

在這個輸入流中跳過并放棄n個字節(jié)的數(shù)據(jù)鸦致。這個skip方法可能由于一些原因跳過數(shù)目較小的字節(jié),可能是0涣楷。如果n是個負數(shù)分唾,那么這個方法會嘗試向后跳過。當(dāng)后備文件不支持在當(dāng)前位置向后跳過時狮斗,這個方法拋出IOException绽乔。該方法返回實際跳過的字節(jié)數(shù)。如果向前跳過碳褒,則返回一個正數(shù)折砸;反之,則返回一個負數(shù)沙峻。

這個方法跳過的字節(jié)數(shù)可能比后備文件中還剩下的多睦授。這不會產(chǎn)生異常,跳過的字節(jié)數(shù)可能包括超出后備文件EOF的一些字節(jié)數(shù)摔寨。跳過EOF嘗試從文件中讀取數(shù)據(jù)會讀到-1去枷。

重寫:

這個方法是InputStream類中Skip方法的重寫。

參數(shù):

n - 被跳過的字節(jié)數(shù)是复。

返回:

該方法返回實際跳過的字節(jié)數(shù)

拋出:

IOException - 如果這個流不支持查找删顶,則會拋出IOException這個錯誤,或則其他I/O錯誤發(fā)生的時候佑笋。


available

public int available() throws IOException

Returns an estimate of number of remaining bytes that can be read(or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. Returns 0 when the file position is beyond EOF. The next invocation might be the same thread of another thread. A single read or skip of this many bytes will not block, but mat read of skip fewer bytes.

In some cases, a non-blocking read(or read) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.

Overides:

available in class InputStream.

Returns:

an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking.

Throws:

IOException - if this file input stream has been closed by calling close or an I/O error occurs.


available

返回對輸入流中可讀取或者可跳過字節(jié)的一個估計翼闹,這個方法在調(diào)用時并不會造成阻塞。它在讀到文件末尾時會返回0蒋纬。下次調(diào)用這個方法可能會在同一個線程里或者在不同的線程里面猎荠。一個單次的read或者skip不會造成阻塞坚弱,但是多次也許會。

重寫:

該方法是InputStream類中available方法的重寫关摇。

返回:

返回對輸入流中可讀或者可跳過字節(jié)書的一個估計荒叶,這個方法不會造成阻塞。

拋出:

IOException - 如果文件的輸入流因為調(diào)用close方法被關(guān)閉了输虱,這個方法會拋出I/O異常些楣。


close

public void close() throws IOException

Closes this file input stream and releases any system resources associated with the stream.

If this stream has an associated channel then the channel is closed as well.

Specified by:

close in interface Closeable.

Specified by:

close in interface AutoCloseable.

Overrides:

close in class InputStream.

Throws:

IOException - if an I/O error occurs.


close

關(guān)閉文件輸入流并且釋放任何相關(guān)的系統(tǒng)資源。

如果這個流和一個channel相關(guān)宪睹,那么這個channel也會被關(guān)閉愁茁。

詳見:

在Closeable接口中的close方法。

詳見:

在AutoCloseable接口中的close方法亭病。

重寫:

該方法是InputStream類中close方法的重寫鹅很。

拋出:

IOException - 當(dāng)發(fā)生I/O錯誤時會拋出IOException


getFD

public final FileDescriptor getFD() throws IOException

Returns the FileDescriptor object that repressents the connection to the actual file in the file system being used by this FileInputStream.

Returns:

the file descriptor object associated with this stream.

Throws:

IOException - if an I/O error occurs.

See Also:

FileDescritpor


返回一個代表實際文件和文件系統(tǒng)中被這個FileInputStream使用的文件之間聯(lián)系的文件描述符對象。

返回:

返回一個跟這個流相關(guān)的文件描述符對象罪帖。

拋出:

IOException - 當(dāng)I/O異常發(fā)生時促煮,拋出一個IOException。


getChannel

public FileChannel getChannel()

Returns the unique FileChannel object associated with this file input stream.

The initial position of the returned channel will be equal to the number of bytes read from the file so far. Reading bytes from this stream will increment the channels's position. Changing the channel's position, either explicitly or by reading ,will change this stream's file position.

Returns:

the file channel associated with this file input stream.

Since:

1.4


返回一個和這個輸入流關(guān)聯(lián)的唯一的FileChannel對象整袁。

該方法返回通道的初始文件位置將會等于在這個文件中已經(jīng)讀取的字節(jié)數(shù)菠齿。繼續(xù)從這個文件中讀取字節(jié)會增加這個通道的位置。不管是直接改變通道的位置或者通過讀寫改變通道的位置都會導(dǎo)致流在文件中的位置坐昙。

返回:

和輸入流相關(guān)的通道绳匀。


finalize

protected void finalized() throws IOException

Ensures that the close method of this file input stream is called when there are no more references to it.

Overrides:

finalized in class Object.

Throws:

IOException - if an I/O error occurs.

See Also:

close()


finalize

確保當(dāng)文件中沒有其他數(shù)據(jù)可讀的時候close方法被調(diào)用。

重寫:

該方法是Object類中finalize方法的重寫民珍。

拋出:

IOException - 當(dāng)I/O錯誤發(fā)生事襟士,拋出一個IOException.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市嚷量,隨后出現(xiàn)的幾起案子陋桂,更是在濱河造成了極大的恐慌,老刑警劉巖蝶溶,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嗜历,死亡現(xiàn)場離奇詭異,居然都是意外死亡抖所,警方通過查閱死者的電腦和手機梨州,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來田轧,“玉大人暴匠,你說我怎么就攤上這事∩嫡常” “怎么了每窖?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵帮掉,是天一觀的道長。 經(jīng)常有香客問我窒典,道長蟆炊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任瀑志,我火速辦了婚禮涩搓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘劈猪。我一直安慰自己昧甘,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布岸霹。 她就那樣靜靜地躺著疾层,像睡著了一般将饺。 火紅的嫁衣襯著肌膚如雪贡避。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天予弧,我揣著相機與錄音刮吧,去河邊找鬼。 笑死掖蛤,一個胖子當(dāng)著我的面吹牛杀捻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蚓庭,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼致讥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了器赞?” 一聲冷哼從身側(cè)響起垢袱,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎港柜,沒想到半個月后请契,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡夏醉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年爽锥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片畔柔。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡氯夷,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出靶擦,到底是詐尸還是另有隱情腮考,我是刑警寧澤擎淤,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站秸仙,受9級特大地震影響嘴拢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜寂纪,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一席吴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捞蛋,春花似錦孝冒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至搬设,卻和暖如春穴店,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拿穴。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工泣洞, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人默色。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓球凰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親腿宰。 傳聞我的和親對象是個殘疾皇子呕诉,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,451評論 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,324評論 0 10
  • 明天又是一年一度考研的日子,給熟知的備考的學(xué)長發(fā)了加油的信息吃度,對方紛紛回復(fù)早已緊張得無法安睡甩挫,想起了去年這個時候,...
    小松肉奈閱讀 123評論 0 1
  • 人生沒有“標尺”规肴,只有“標桿”捶闸。 堂堂正正做人,踏踏實實做事拖刃。這樣的人生就應(yīng)該成為世人為之效仿的標桿删壮。 一句話,簡...
    高原野菊花閱讀 161評論 0 3
  • 路過街邊的小吃鋪兑牡。 ‘’春卷看起來很香的樣子央碟,哎,還是算了∫谒洌‘’我保持勻速菱涤,頭也沒回地自言自語道。正想和旁邊的基友...
    果子村放映隊大隊長V閱讀 227評論 0 0