使用InputStream从内部存储读取文件 [英] Using InputStream to read file from internal storage
问题描述
我搜索了几天,发现所有内容都是使用bufferedReader从内部存储中的文件读取的.不能使用InputStream读取内部存储中的文件吗?
I've searched for a couple days, and all I find is using bufferedReader to read from a file on internal storage. Is it not possible to use InputStream to read from a file on internal storage?
private void dailyInput()
{
InputStream in;
in = this.getAsset().open("file.txt");
Scanner input = new Scanner(new InputStreamReader(in));
in.close();
}
我现在将其与input.next(
一起使用)以在文件中搜索所需的数据.一切正常,但是我想将新文件保存到内部存储中并从中读取,而无需将所有内容都更改为bufferedReader.这可能吗?还是我需要硬着头皮改变一切?仅供参考,我不需要写,只需阅读.
I use this now with input.next(
) to search my file for the data that I need. It all works fine, but I would like to save new files to internal storage and read from them without changing everything to bufferedReader. Is this possible or do I need to bite the bullet and change everything? FYI I don't need to write, only read.
推荐答案
写入文件.
String FILENAME = "file.txt";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
阅读
void OpenFileDialog(String file) {
//Read file in Internal Storage
FileInputStream fis;
String content = "";
try {
fis = openFileInput(file);
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {
}
content += new String(input);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
content
将包含您的文件数据.
content
will Contain your File Data.
这篇关于使用InputStream从内部存储读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!