一、FileInputStreamReader
FileInputStreamReader (一般使用子類 FileReader) 是 Java 中的一個用於讀取文件的類。該類是 InputStreamReader 的子類,它按照指定編碼方式從文件中讀取字元。
FileReader相對於 InputStreamReader來說查找文件不需要指定編碼方式,它會按照文件的默認編碼方式讀取
FileInputStreamReader常用構造函數:
public InputStreamReader(InputStream in);
public InputStreamReader(InputStream in, String charsetName);
第一個構造函數是通過給定的 InputStream 對象和默認字符集來創建對象。第二個構造函數是構造一個根據指定字符集來讀取流的 InputStreamReader。
二、FileInputStreamRead讀取文件
FileInputStreamRead 是用於從文件中讀取位元組流的類,用於讀取位元組流時,一般會結合 BufferedReader 類和 InputStreamReader 類來使用。
讀取的核心方法為 read(byte[] b) 和 read(byte[] b, int off, int len)。
第一個 read 方法從輸入流中讀取一些位元組,並將它們存儲到緩衝區數組 b 中。第二個 read 方法是從流中讀取 len 個位元組,並將其存儲在偏移量 off 的緩衝區數組 b 中。
讀取文件的代碼示例:
public static void main(String[] args){
try {
FileInputStream fis = new FileInputStream("test.txt"); // 創建位元組輸入流
byte[] data = new byte[1024]; // 創建一個 bytes 數組,用來存儲讀取的數據
int len = fis.read(data); // 讀取數據
String result = new String(data, 0, len, "UTF-8"); // 解碼讀取到的數據(默認以 UTF-8 解碼)
System.out.println(result); // 列印結果
} catch (Exception e) {
e.printStackTrace();
}
}
三、FileInputStreamRead用法
FileInputStreamRead 有很多用法,下面介紹幾個常用的。
1. 讀取文件到位元組數組中
將文件讀取到位元組數組中,可以自定義緩衝區大小,從而提高讀取效率。
public static byte[] readFileToByteArray(File file) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int readLength;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((readLength = fis.read(buffer)) != -1) {
bos.write(buffer, 0, readLength);
}
return bos.toByteArray();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// Ignore exception.
}
}
}
}
2. 讀取文件到字元緩衝區中
在文件較大且需要一次讀取全部數據的情況下,可以使用 BufferedReader 來提高讀取效率,同時可以通過指定字符集來解決讀取中文時亂碼的問題。
public static void readFileToStringBuffer(File file) throws IOException {
FileInputStream fis = null;
BufferedReader reader = null;
try {
fis = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// Ignore exception.
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// Ignore exception.
}
}
}
}
3. 讀取壓縮文件
利用 Java 自帶的 GZIPInputStream 類,可以讀取壓縮後的文件。
public static void readCompressedFile(File file) throws IOException {
GZIPInputStream gis = null;
try {
gis = new GZIPInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} finally {
if (gis != null) {
try {
gis.close();
} catch (IOException e) {
// Ignore exception.
}
}
}
}
4. 讀取網路文件
通過 URL 類從網路上讀取文件,通常用於獲取需要下載的文件的鏈接,然後使用 HttpURLConnection 類下載文件。
public static void readRemoteFile(String url) throws IOException {
InputStream in = new URL(url).openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
in.close();
}
四、總結
FileInputStreamRead 是 Java 中用於讀取文件的一個重要類,其使用範圍廣泛。其中,FileInputStreamReader 用於按指定編碼方式從文件中讀取字元,而 FileInputStreamRead 用於讀取位元組流。
本文介紹了 FileInputStreamRead 的一些常用用法,包括將文件讀取到位元組數組中、將文件讀取到字元緩衝區中、讀取壓縮文件以及從網路上讀取文件等用法。對於需要從文件讀取數據的 Java 開發人員來說,掌握 FileInputStreamRead 是非常重要的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242269.html
微信掃一掃
支付寶掃一掃