使用inputStream 写一个获取本地文件的代码
时间: 2024-05-07 09:16:14 浏览: 73
以下是使用Java的InputStream获取本地文件的示例代码:
```java
import java.io.*;
public class ReadLocalFile {
public static void main(String[] args) {
try {
// 创建一个FileInputStream对象
FileInputStream fileInputStream = new FileInputStream("文件路径");
// 创建一个字节数组用于存储读取的数据
byte[] bytes = new byte[1024];
// 用于存储读取的字节数
int length;
// 读取文件内容并输出
while ((length = fileInputStream.read(bytes)) != -1) {
System.out.write(bytes, 0, length);
}
// 关闭流
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
其中,需要将“文件路径”替换为本地文件的实际路径。这段代码使用FileInputStream类打开一个本地文件,然后使用read()方法从文件中读取数据,并将读取的内容输出到控制台。最后,在读取完文件内容之后关闭文件流。
阅读全文
相关推荐
















