闽公网安备 35020302035485号
前言:
InputStream是一个抽象类,它是所有字节输入流实现类的基类。它的主要作用是抽象地表示从不同数据源产生输入的类,例如FileInputStream、FilterInputStream等。InputStream提供了一些常用的方法,如read()、read(byte[] b)等,用于从数据源中读取字节数据。它还提供了一些其他方法,如available()、skip()等,用于控制读取的数据量和处理跳过部分数据等操作。InputStream通常与OutputStream类配合使用,实现数据的读取和写入。今天我们就详解一下InputStream的几个实现类的使用方式。
一、FileInputStream的代码示例
import java.io.*;
public class FileInputStreamExample {
public static void main(String[] args) {
// 堆代码 duidaima.com
// 要读取的文件路径和名称
String filePath = "C:/example/file.txt";
// 创建输入流对象
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
int len;
// 使用 while 循环读取文件,每次最多读取 1024 个字节
while ((len = fis.read(buffer)) != -1) {
// 将读取的字节转换为字符串,并输出到控制台
String content = new String(buffer, 0, len, "UTF-8");
System.out.println(content);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filePath);
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
} finally {
// 关闭输入流
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
System.out.println("Error closing file: " + e.getMessage());
}
}
}
}
示例代码说明:注意:在使用FileInputStream类时,我们需要确保文件存在,并且我们有读取文件的权限。此外,在实际应用中,可能需要根据需要使用更高效的方法读取大型文件,以避免IO开销的问题。
import java.io.*;
public class ByteArrayInputStreamExample {
public static void main(String[] args) {
byte[] bytes = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 };
// 创建字节输入流对象
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try {
byte[] buffer = new byte[1024];
int len;
// 使用 while 循环读取字节数组中的内容,每次最多读取 1024 个字节
while ((len = bais.read(buffer)) != -1) {
// 将读取的字节转换为字符串,并输出到控制台
String content = new String(buffer, 0, len, "UTF-8");
System.out.println(content);
}
} catch (IOException e) {
System.out.println("Error reading byte array: " + e.getMessage());
} finally {
// 关闭输入流
try {
if (bais != null) {
bais.close();
}
} catch (IOException e) {
System.out.println("Error closing byte array input stream: " + e.getMessage());
}
}
}
}
示例代码说明:import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedInputStreamExample {
public static void main(String[] args) throws Exception {
// 创建一对PipedInputStream和PipedOutputStream
PipedInputStream input = new PipedInputStream();
PipedOutputStream output = new PipedOutputStream(input);
// 创建一个写线程
Thread writerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
// 写入一些数据到PipedOutputStream
output.write("Hello, World!".getBytes());
output.close(); // 关闭PipedOutputStream
} catch (IOException e) {
e.printStackTrace();
}
}
});
// 创建一个读线程
Thread readerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
// 读取PipedInputStream中的数据
int data;
while ((data = input.read()) != -1) {
System.out.print((char) data); // 将数据打印到控制台
}
input.close(); // 关闭PipedInputStream
} catch (IOException e) {
e.printStackTrace();
}
}
});
// 启动写线程和读线程
writerThread.start();
readerThread.start();
// 等待写线程和读线程完成
writerThread.join();
readerThread.join();
}
}
代码说明