一、InputStream簡介
InputStream是Java標準庫中的一個抽象類,它用於讀取二進位數據流。所有實現了該抽象類的子類都具有讀取數據的能力。InputStream主要提供了以下幾個方法:
- int read():讀取單個位元組數據,並返回其int形式,如果沒有數據可讀,則返回-1
- int read(byte[] b):讀取多個位元組數據,並將其存儲到位元組數組b中,返回實際讀取到的位元組數
- int read(byte[] b, int off, int len):讀取多個位元組數據,並將其存儲到位元組數組b中,從位元組數組的off位置開始寫入,最多寫入len個位元組,返回實際讀取到的位元組數
- void close():關閉InputStream流,釋放與其相關聯的所有資源
通過關鍵字new創建InputStream類的子類的對象,並使用其中的read()方法,即可實現將數據轉換成bytes的操作。
二、使用InputStream讀取本地文件
在Java中,可以使用java.io.File類來表示本地文件。下面是使用InputStream讀取本地文件的示例代碼:
File file = new File("C:\\test\\test.txt"); InputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes); inputStream.close();
代碼中,首先通過File類構造函數創建File對象表示要讀取的文件,然後使用FileInputStream類的構造方法創建文件輸入流。在文件輸入流中提供了read方法讀取二進位數據,將數據寫入位元組數組中,最後關閉文件輸入流。
三、使用InputStream讀取網路數據
在Java中可以使用java.net包中的URLConnection類來獲取網路數據。下面是使用InputStream讀取網路數據的示例代碼:
URL url = new URL("http://www.baidu.com"); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes); inputStream.close();
代碼中,首先使用URL類創建一個URL對象,代表想要訪問的網路資源。然後使用openConnection方法創建一個到該URL的連接,獲取URLConnection實例。最後,使用URLConnection的getInputStream方法獲取輸入流,並使用read方法讀取網路資源的二進位數據,將數據寫入位元組數組中,關閉輸入流。
四、使用InputStream及BufferedInputStream讀取文件
讀取文件時,使用BufferedInputStream可以提高讀取效率,減少磁碟IO次數。BufferedInputStream是可以帶緩衝區的輸入位元組流,它在讀取一個位元組時,會盡量多地去讀取一部分數據並放到緩衝區中。
File file = new File("C:\\test\\test.txt"); InputStream inputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); byte[] bytes = new byte[1024]; int len; while ((len = bufferedInputStream.read(bytes)) != -1) { // TODO: 對讀取到的數據進行處理 } bufferedInputStream.close(); inputStream.close();
在代碼中,首先使用File類創建File對象,再使用FileInputStream類的構造方法創建文件輸入流,最後使用BufferedInputStream類包裹輸入流,讀取文件時使用緩衝區,提高讀取效率。
五、使用InputStream及ByteArrayOutputStream讀取網路數據
ByteArrayOutputStream是一個可以根據需要自動增長的內存緩衝區。在讀取網路數據時,常常需要先將其下載到內存緩衝區中,然後再進行其他處理。使用ByteArrayOutputStream可以實現很方便地將數據讀取到內存緩衝區中的操作。
URL url = new URL("http://www.baidu.com"); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, len); } byte[] bytes = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); inputStream.close();
代碼中,通過URL類創建URL對象表示要訪問的網路資源,使用openConnection方法創建連接,獲取URLConnection實例,使用getInputStream方法獲取輸入流,然後使用ByteArrayOutputStream將數據讀取到內存緩衝區中。在緩衝區中讀取數據時,可將緩衝區的容量設置得比數據量大,避免緩衝區滿後的擴容操作。
六、使用InputStream及DataInputStream讀取二進位數據
在Java中,可以使用DataInputStream類對二進位數據進行讀取。DataInputStream是一個數據輸入流,它允許應用程序以與機器無關方式從底層輸入流中讀取基本Java數據類型。例如,可以使用DataInputStream讀取int、float、double等類型的數據。
File file = new File("C:\\test\\test.bin"); InputStream inputStream = new FileInputStream(file); DataInputStream dataInputStream = new DataInputStream(inputStream); int value1 = dataInputStream.readInt(); // 讀取int類型 float value2 = dataInputStream.readFloat(); // 讀取float類型 double value3 = dataInputStream.readDouble(); // 讀取double類型 byte[] value4 = new byte[1024]; dataInputStream.read(value4); // 讀取位元組數組 dataInputStream.close(); inputStream.close();
代碼中,首先使用File類創建File對象,然後使用FileInputStream類的構造方法創建文件輸入流,再使用DataInputStream類包裹輸入流。在使用DataInputStream讀取數據時,需指定數據類型的長度。在讀取位元組數組時,需要新建一個byte數組。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/230404.html