一、InputStreamString簡介
InputStreamString是Java IO庫中的一個類,它是字節輸入流和字符輸入流之間的橋樑,可以將字節流轉化成字符流,方便程序進行處理。在Java中,數據的輸入和輸出都是通過流的形式進行的,數據可以從源中被讀出,然後被寫入到目標中。而InputStreamString就是用來讀取來自輸入流的數據的。
下面就讓我們通過幾個方面來詳細介紹InputStreamString類的使用。
二、InputStreamString的常用方法
1、構造函數
InputStreamString類有兩個常用的構造函數:
public InputStreamString(InputStream is); public InputStreamString(InputStream is, Charset charset);
第一個構造函數只接受兩個字節輸入流(即InputStream),不指定字符集,將使用平台默認字符集進行編碼。第二個構造函數指定了字符集編碼方式。
2、read()方法
read()是InputStreamString中最基本的讀取方法,它會從輸入流中讀取一個字節的數據。如果讀取到了數據,則返回該數據;否則返回-1。此方法可能會阻塞,直到有可用的數據為止。
public int read() throws IOException
3、read(byte[] b)方法
從輸入流中讀取一定數量的字節,並將其存儲到緩衝區數組b中。如果成功讀取了數據,此方法返回實際讀取的字節數。如果沒有可用的數據,則返回-1。此方法可能會阻塞,直到有可用的數據為止。
public int read(byte[] b) throws IOException
4、read(byte[] b, int off, int len)方法
從輸入流中讀取最多len個字節的數據到緩衝區數組b中。數據從偏移量off開始存儲,並返回實際讀取的字節數。如果沒有可用的數據,則返回-1。此方法可能會阻塞,直到有可用的數據為止。
public int read(byte[] b, int off, int len) throws IOException
5、available()方法
此方法返回在沒有阻塞的情況下可以從輸入流中讀取(跳過)的字節數。注意,返回的值可能大於實際可用的字節數,因此這裡僅作為參考。
public int available() throws IOException
三、InputStreamString的使用示例
1、從文件中讀取內容
下面的代碼演示了如何使用InputStreamString從文件中讀取文本內容。文件編碼使用的是UTF-8格式。
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadFile {
public static void main(String[] args) {
try {
File file = new File("hello.txt");
InputStream inputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
char[] buffer = new char[(int) file.length()];
inputStreamReader.read(buffer);
String content = new String(buffer);
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、從網絡上讀取內容
下面的代碼演示了如何使用InputStreamString從網絡上讀取Web頁面內容。
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class ReadWebPage {
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com");
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int length = -1;
StringBuffer stringBuffer = new StringBuffer();
while ((length = inputStream.read(buffer)) != -1) {
stringBuffer.append(new String(buffer, 0, length));
}
System.out.println(stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、從字符串中讀取內容
下面的代碼演示了如何使用InputStreamString從字符串中讀取內容。
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadString {
public static void main(String[] args) {
try {
String str = "Hello World!\n中文測試";
InputStream inputStream = new ByteArrayInputStream(str.getBytes());
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
char[] buffer = new char[str.length()];
inputStreamReader.read(buffer);
String content = new String(buffer);
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/286957.html
微信掃一掃
支付寶掃一掃