openFileInput()和/或openFileOutput()I / O流默默地失败 [英] openFileInput() and/or openFileOutput() i/o streams silently failing

查看:166
本文介绍了openFileInput()和/或openFileOutput()I / O流默默地失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与Android平台打打闹闹,与存储数据的方式不同乱搞。现在我使用上下文方法 openFileInput() openFileOutput()

I've been fooling around with the android platform, messing around with different ways of storing data. Right now I am using the Context methods openFileInput() and openFileOutput().

我创建了一个名为默认只为这两种方法的文档告诉我的文件。下面是一些例子code(这些例子中我所做的复制品,我知道文件名和变量名称不同):

I created a file called default just as the documentation for these two methods told me. Here's some example code (these examples are replicas of what I did, I am aware that the filename and variables are named differently):

openFileOutput()...

openFileOutput()...

    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();

openFileInput()...

openFileInput()...

    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();

在这些code段,世界你好应写入文件hello_file,而应该是什么在 STR 。我和我的code遇到的问题是,不管什么我写信给我的文件,在 FileInputReader 拿起什么都没有。

In these code snippets, "hello world" should be written to the file "hello_file", and should be what's in str. The problem I'm having with my code is that no matter what I write to my file, the FileInputReader picks up nothing.

我通过Android文档中列出的权限滚动,但我无法找到有关内部存储任何东西(和我pretty确定你不需要许可这样的事情)。

I scrolled through the permissions listed in the android documentation, but I could not find anything about internal storage (and I am pretty sure you don't need a permission for something like this).

底线是,我不明白为什么 FileInputWriter 不写任何东西,或为什么 FileInputReader 不读任何东西(我不知道它是)时,code运行正常,没有错误。

The bottom line is, I don't understand why the FileInputWriter isn't writing anything or why the FileInputReader isn't reading anything (I don't know which it is) when the code runs fine and without error.

推荐答案

我写回的答案,因为这是得到太多的评论。
我试过你的code - 很好,它只是正常工作

I write back as answer, because this is getting too much for a comment. I've tried your code - and well, it just works fine.

我能想象的唯一的事情是,你的设备有问题。在这种情况下,我会期待一些例外......结果
你仍然可以做,是复制我的code和检查日志。看看它是否工作,或者你可能会得到一些例外。然后检查你在你的设备有多少内存(它是真实的还是模拟的?)结果
如果是模拟它可能是,即使这样的小文件太小。

The only thing I can imagine is, that your device has a problem. In which case I would expect some exception...
What you still can do, is to copy my code and check the log. See if it works, or if you might get some exception. Then check how much internal memory you got in your device (is it real or emulated?)
If it is emulated it might be too small for even such a small file.

下面是我的code,我投入 onResume()

Here's my code, that I put into onResume()

    String FILENAME = "hello_file";
    String string = "hello world!";

    try {
        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e("STACKOVERFLOW", e.getMessage(), e);
    }

    try {
        FileInputStream fis = 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();
        Log.i("STACKOVERFLOW", String.format("GOT: [%s]", str));
    } catch (IOException e) {
        Log.e("STACKOVERFLOW", e.getMessage(), e);
    }

输出:

08-16 08:31:38.748: I/STACKOVERFLOW(915): GOT: [hello world!]

有一类:有用,但不能求解广义的问题。

Is there a category of: "Helpful, but not sovling the problem"?

这篇关于openFileInput()和/或openFileOutput()I / O流默默地失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆