java - 如何在我的方法中获取已经存在的文件(在我的计算机上)并将其转换为 InputStream?
问题描述
我在 D:/EXPORT_BASE/Export_report 中有一个生成的文件。我需要做的是使用 filePath 字符串从本地获取此文件,然后将其转换为 InputStream。
String filePath = D:/EXPORT_BASE/Export_report/1557834965979_report.txt
我需要使用字符串来获取文件并将其写入 InputStream。
解决方案
File file = new File("C:/myTest.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
System.out.println("Total file size to read (in bytes) : "
+ fis.available());
int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
推荐阅读
- javascript - 不在状态中设置属性?
- go - Golang:原子读取有什么用?
- cadence-workflow - 如何在不破坏确定性的情况下对 Uber Cadence 工作流程进行更改或修复?
- react-redux - Redux Observable 史诗,设置动作之间的超时间隔
- python-3.x - 列表字典上的 Python3 itertools.permutations?
- elasticsearch - 如何使用logstash动态定义elasticsearch索引?
- django - Django 模板响应中间件
- apache-spark - Jupyter Notebook 中的 Spark Streaming awaitTermination
- java - 单击关闭下拉菜单时如何查找元素定位器
- c# - 不了解 wpf 更新动态创建的图像控件可见性每秒变化的行为