使用Java InputStream讀取文件

一、InputStream概述

InputStream是Java IO中用於讀取位元組流的抽象類。它是所有輸入流的超類,可以從文件、網路、內存等任何來源中讀取位元組流。

InputStream中最常用的方法是read(),用於讀取下一個位元組,返回的是位元組數據的整數值,如果已到達流的末尾,則返回-1。

二、InputStrem使用

使用InputStream讀取文件數據需要以下步驟:

1、打開文件:使用InputStream打開指定文件。

2、讀取數據:使用read()方法讀取文件數據。

3、關閉文件:使用InputStrem關閉文件。

public class InputStreamExample {
    public static void main(String[] args) {
        try {
            FileInputStream inputStream = new FileInputStream("test.txt");
            int data;
            while ((data = inputStream.read()) != -1) {
                System.out.print((char) data);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

三、緩衝流BufferedInputStream

InputStream每次只能讀取一個位元組,如果需要讀取大量數據,則需要多次讀取,效率低下。這時可以使用緩衝流BufferedInputStream,在InputStream的基礎上增加了一個緩衝區,使得可以批量讀取數據,提高了讀取效率。

public class BufferedInputStreamExample {
    public static void main(String[] args) {
        try {
            FileInputStream inputStream = new FileInputStream("test.txt");
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            int data;
            while ((data = bufferedInputStream.read()) != -1) {
                System.out.print((char) data);
            }
            bufferedInputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

四、位元組數組InputStream

除了使用一個位元組一個位元組地讀取文件數據,也可以直接將整個文件數據讀入一個位元組數組中,再進行處理。

public class ByteArrayInputExample {
    public static void main(String[] args) {
        String str = "Hello World!";
        byte[] byteArray = str.getBytes();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        int data;
        while ((data = byteArrayInputStream.read()) != -1) {
            System.out.print((char) data);
        }
        byteArrayInputStream.close();
    }
}

五、文件IO異常處理

在讀取文件的過程中,可能會遇到文件不存在、無法訪問、許可權不夠等異常。為了避免程序崩潰,需要對異常進行捕獲和處理。

public class InputStreamEnsureExample {
    public static void main(String[] args) {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream("test.txt");
            int data;
            while ((data = inputStream.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

六、小結

Java InputStream提供了讀取文件數據的基礎功能,能夠從文件、網路等不同來源中讀取位元組數據。使用InputStream讀取文件數據需要遵循打開、讀取、關閉的基本步驟,可以結合緩衝流BufferedInputStream、位元組數組InputStream等進行批量讀取數據,提高讀取效率。同時在讀取文件數據時需要處理可能出現的異常,並最終保證文件流的關閉。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/154321.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-15 07:15
下一篇 2024-11-16 14:11

相關推薦

發表回復

登錄後才能評論