一、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/n/286957.html