我一直在玩弄安卓平台,玩弄不同的数据存储方式。现在我正在使用Context
方法openFileInput()
和openFileOutput()
.
正如这两种方法的文档告诉我的那样,我创建了一个名为 default 的文件。这是一些示例代码(这些示例是我所做的复制品,我知道文件名和变量的名称不同):
打开文件输出()...
Context cont = /* defined somewhere */;
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = cont.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.flush();
fos.close();
打开文件输入()...
FileInputStream fis = cont.openFileInput("hello_file");
byte[] buffer = new byte[(int) fis.getChannel().size()];
fis.read(buffer);
String str= "";
for(byte b:buffer) str+=(char)b;
fis.close();
在这些代码片段中,“hello world”应该写入文件“hello_file”,并且应该是str
. 我的代码遇到的问题是,无论我写什么到我的文件中,FileInputReader
它什么都没有。
我滚动浏览了 android 文档中列出的权限,但我找不到任何有关内部存储的信息(而且我很确定您不需要此类权限)。
底线是,当代码运行良好且没有错误时,我不明白为什么FileInputWriter
不写任何东西或为什么FileInputReader
不读任何东西(我不知道它是什么)。