<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java `FileInputStream`類 > 原文: [https://www.programiz.com/java-programming/fileinputstream](https://www.programiz.com/java-programming/fileinputstream) #### 在本教程中,我們將借助示例學習 Java `FileInputStream`及其方法。 `java.io`包的`FileInputStream`類可用于從文件中讀取數據(以字節為單位)。 它擴展了`InputStream`抽象類。 ![Java FileInputStream is a subclass of InputStream class.](https://img.kancloud.cn/da/5b/da5b5988983ce67621bf9af93c820762_416x372.png "Java FileInputStream Class") 在學習`FileInputStream`之前,請確保了解 [Java 文件](/java-programming/file "Java Files")。 * * * ## 創建一個`FileInputStream` 為了創建文件輸入流,我們必須首先導入`java.io.FileInputStream`包。 導入包后,就可以使用 Java 創建文件輸入流。 **1.使用文件路徑** ```java FileInputStream input = new FileInputStream(stringPath); ``` 在這里,我們創建了一個輸入流,該輸入流將鏈接到`path`指定的文件。 **2.使用文件對象** ```java FileInputStream input = new FileInputStream(File fileObject); ``` 在這里,我們創建了一個輸入流,該輸入流將鏈接到`fileObject`指定的文件。 * * * ## `FileInputStream`的方法 `FileInputStream`類提供了`InputStream`類中存在的不同方法的實現。 ### `read()`方法 * `read()` - 從文件中讀取一個字節 * `read(byte[] array)` - 從文件中讀取字節并將其存儲在指定的數組中 * `read(byte[] array, int start, int length)` - 從文件中讀取等于`length`的字節數,并從位置`start`開始存儲在指定的數組中 假設我們有一個名為`input.txt`的文件,其內容如下。 ```java This is a line of text inside the file. ``` 讓我們嘗試使用`FileInputStream`讀取此文件。 ```java import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { FileInputStream input = new FileInputStream("input.txt"); System.out.println("Data in the file: "); // Reads the first byte int i = input.read(); while(i != -1) { System.out.print((char)i); // Reads next byte from the file i = input.read(); } input.close(); } catch(Exception e) { e.getStackTrace(); } } } ``` **輸出** ```java Data in the file: This is a line of text inside the file. ``` 在上面的示例中,我們創建了一個名為`input`的文件輸入流。 輸入流與`input.txt`文件鏈接。 ```java FileInputStream input = new FileInputStream("input.txt"); ``` 為了從文件中讀取數據,我們在`while`循環中使用了`read()`方法。 * * * ### `available()`方法 要獲得可用字節數,我們可以使用`available()`方法。 例如, ```java import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { // Suppose, the input.txt file contains the following text // This is a line of text inside the file. FileInputStream input = new FileInputStream("input.txt"); // Returns the number of available bytes System.out.println("Available bytes at the beginning: " + input.available()); // Reads 3 bytes from the file input.read(); input.read(); input.read(); // Returns the number of available bytes System.out.println("Available bytes at the end: " + input.available()); input.close(); } catch (Exception e) { e.getStackTrace(); } } } ``` **輸出**: ```java Available bytes at the beginning: 39 Available bytes at the end: 36 ``` 在上面的示例中, 1. 我們首先使用`available()`方法檢查文件輸入流中的可用字節數。 2. 然后,我們已使用`read()`方法 3 次從文件輸入流中讀取 3 個字節。 3. 現在,在讀取字節之后,我們再次檢查了可用字節。 這次,可用字節減少了 3。 * * * ### `skip()`方法 要丟棄并跳過指定的字節數,可以使用`skip()`方法。 例如, ```java import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { // Suppose, the input.txt file contains the following text // This is a line of text inside the file. FileInputStream input = new FileInputStream("input.txt"); // Skips the 5 bytes input.skip(5); System.out.println("Input stream after skipping 5 bytes:"); // Reads the first byte int i = input.read(); while (i != -1) { System.out.print((char) i); // Reads next byte from the file i = input.read(); } // close() method input.close(); } catch (Exception e) { e.getStackTrace(); } } } ``` **輸出**: ```java Input Stream after skipping 5 bytes: is a line of text inside the file. ``` 在上面的示例中,我們使用`skip()`方法從文件輸入流中跳過 5 個字節的數據。 因此,不會從輸入流中讀取代表文本`This`的字節。 * * * ### `close()`方法 要關閉文件輸入流,可以使用`close()`方法。 調用`close()`方法后,我們將無法使用輸入流讀取數據。 在以上所有示例中,我們都使用`close()`方法關閉文件輸入流。 * * * ## `FileInputStream`的其他方法 | 方法 | 內容描述 | | --- | --- | | `finalize()` | 確保調用`close()`方法 | | `getChannel()` | 返回與輸入流關聯的`FileChannel`的對象 | | `getFD()` | 返回與輸入流關聯的文件描述符 | | `mark()` | 標記輸入流中已讀取數據的位置 | | `reset()` | 將控件返回到輸入流中設置了標記的點 | 要了解更多信息,請訪問 [Java `FileInputStream`(官方 Java 文檔)](https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html "Java FileInputStream (official Java documentation)")。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看