Java 中 FileInputStream 和 FileOutputStream 類有什麼作用?
Java 提供 I/O 流來讀寫資料,其中流代表輸入源或輸出目標,可以是檔案、I/O 裝置或其他程式等。
有兩種型別的流可用:
- InputStream - 用於從源讀取(順序)資料。
- OutputStream - 用於向目標寫入資料。
FileInputStream
此類從特定檔案(逐位元組)讀取資料。它通常用於讀取包含原始位元組的檔案內容,例如影像。
要使用此類讀取檔案內容:
- 首先,需要透過傳遞一個字串變數或 File 物件來例項化此類,該變數或物件代表要讀取的檔案的路徑。
FileInputStream inputStream = new FileInputStream("file_path");
or,
File file = new File("file_path");
FileInputStream inputStream = new FileInputStream(file);- 然後使用 read() 方法的任何變體讀取指定檔案的內容:
int read() - 只需從當前 InputStream 讀取資料並逐位元組(以整數格式)返回讀取的資料。
如果到達檔案末尾,此方法返回 -1。
int read(byte[] b) - 此方法接受一個位元組陣列作為引數,並將當前 InputStream 的內容讀取到給定的陣列中。
此方法返回一個整數,表示位元組總數或如果到達檔案末尾則返回 -1。
int read(byte[] b, int off, int len) - 此方法接受一個位元組陣列、其偏移量 (int) 和其長度 (int) 作為引數,並將當前 InputStream 的內容讀取到給定的陣列中。
此方法返回一個整數,表示位元組總數或如果到達檔案末尾則返回 -1。
示例
假設我們在 D:/images 目錄中有一個影像

以下程式使用 FileInputStream 讀取上述影像的內容。
示例
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample {
public static void main(String args[]) throws IOException {
//Creating a File object
File file = new File("D:/images/javafx.jpg");
//Creating a FileInputStream object
FileInputStream inputStream = new FileInputStream(file);
//Creating a byte array
byte bytes[] = new byte[(int) file.length()];
//Reading data into the byte array
int numOfBytes = inputStream.read(bytes);
System.out.println("Data copied successfully...");
}
}輸出
Data copied successfully...
FileOutputStream
這將資料寫入特定檔案或檔案描述符(逐位元組)。它通常用於寫入包含原始位元組的檔案內容,例如影像。
要使用此類寫入檔案內容:
- 首先,需要透過傳遞一個字串變數或 File 物件來例項化此類,該變數或物件代表要讀取的檔案的路徑。
FileOutputStream outputStream = new FileOutputStream("file_path");
or,
File file = new File("file_path");
FileOutputStream outputStream = new FileOutputStream (file);還可以透過傳遞 FileDescriptor 物件來例項化 FileOutputStream 類。
FileDescriptor descriptor = new FileDescriptor(); FileOutputStream outputStream = new FileOutputStream(descriptor);
- 然後使用 write() 方法的任何變體將資料寫入指定檔案:
- int write(int b) - 此方法接受單個位元組並將其寫入當前 OutputStream。
- int write(byte[] b) - 此方法接受一個位元組陣列作為引數,並將其中的資料寫入當前 OutputStream。
- int write(byte[] b, int off, int len) - 此方法接受一個位元組陣列、其偏移量 (int) 和其長度 (int) 作為引數,並將它的內容寫入當前 OutputStream。
示例
假設我們在 D:/images 目錄中有一個影像

以下程式讀取上述影像的內容並使用 FileOutputStream 類將其寫回另一個檔案。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamExample {
public static void main(String args[]) throws IOException {
//Creating a File object
File file = new File("D:/images/javafx.jpg");
//Creating a FileInputStream object
FileInputStream inputStream = new FileInputStream(file);
//Creating a byte array
byte bytes[] = new byte[(int) file.length()];
//Reading data into the byte array
int numOfBytes = inputStream.read(bytes);
System.out.println("Data copied successfully...");
//Creating a FileInputStream object
FileOutputStream outputStream = new FileOutputStream("D:/images/output.jpg");
//Writing the contents of the Output Stream to a file
outputStream.write(bytes);
System.out.println("Data written successfully...");
}
}輸出
Data copied successfully... Data written successfully...
如果驗證給定的路徑,您可以觀察到生成的影像為:

廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP