简体   繁体   English

带有android.os.memoryfile的文件IO

[英]File IO with android.os.memoryfile

I am trying to write to a file using android.os.memoryfile and then read from the same file. 我正在尝试使用android.os.memoryfile写入文件,然后从同一文件读取。 I write to the file in one method and read from another method, all in the same class. 我用一种方法写入文件,而从另一种方法读取,都在同一类中。 I can write fine but When I read the file I get lines of a symbol instead of the content I had written to the file. 我可以写得很好,但是当我读取文件时,得到的是符号行,而不是我写入文件的内容。 It seems that the read method is not reading the file I had written to. 似乎read方法没有读取我写入的文件。

If I put code to write and read the file in same method, it seems to work fine. 如果我用相同的方法编写代码来读写文件,它似乎可以正常工作。 Reading file outputs the string I had written. 读取文件将输出我编写的字符串。

Here is my code: 这是我的代码:

    public class FileActivity extends Activity {

        MemoryFile memFile;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            memFile = new MemoryFile("MemoryFileTest", 1000000);
        } catch (IOException e) {
            e.printStackTrace();
        }
     }



      public void readFile () {
            StringBuffer strBuffer = new StringBuffer();

            try {

            InputStream in = memFile.getInputStream();
            BufferedReader reader = new BufferedReader(new  InputStreamReader(in));

           String inputString;
           while ((inputString = reader.readLine()) != null) {
                strBuffer.append(inputString + "\n");
           }

           in.close();
           reader.close();

             Log.d(TAG, strBuffer.toString());
          } catch (IOException e) {
              e.printStackTrace();
          }
    } 

    public void writeFile () {
         String example = "This is an example";
         byte[] bytes = example.getBytes();
         try {
              OutputStream out = memFile.getOutputStream();
              out.write(bytes);

              out.close();
         }  catch (IOException e) {
            e.printStackTrace();
         }
     }
}

In the above code, the MemoryFile is declared in top of class, and initialized in onCreate() method. 在上面的代码中,MemoryFile在类的顶部声明,并在onCreate()方法中初始化。 I am then trying to write and read with that one MemoryFile. 然后,我尝试使用该MemoryFile进行读写。

What am I missing here? 我在这里想念什么?

I've not used MemoryFile and only starting learning about it for this question. 我没有用过MemoryFile ,只是开始学习这个问题。 I looked at the source code from within Android Studio. 我在Android Studio中查看了源代码。 I couldn't find the raw source on the Web. 我在网上找不到原始资源。 The grepcode version is here . grepcode版本在这里 From my experiments and looking at the source code, it's clear that the end-of-file concepts we expect from file-based streams don't apply to a MemoryFile . 通过我的实验并查看源代码,很明显,我们期望基于文件的流中的文件结尾概念不适用于MemoryFile The end-of-file is based on the length specified in the MemoryFile constructor, not the number of bytes that have been written to the file. 文件的结尾基于MemoryFile构造函数中指定的长度,而不是已写入文件的字节数。 Given this, the behavior of BufferedReader.readline() , which you are using in your code, is not well defined because it is expecting to find an end-of-file condition at the end of the character stream and it won't get that with a MemoryFile until it gets to the end of allocated memory buffer. 鉴于此,您在代码中使用的BufferedReader.readline()的行为没有得到很好的定义,因为它期望在字符流的末尾找到文件结束条件,并且不会得到直到它到达分配的内存缓冲区的末尾为止。 This explains why you see all the diamond question marks in the logcat output. 这解释了为什么您在logcat输出中看到所有菱形问号。

To get a better understanding of the end-of-file behavior, you can modify readFile() like this. 为了更好地了解文件结束行为,您可以像这样修改readFile() You will see that 'n' is the length you specify in the MemoryFile constructor. 您会看到“ n”是您在MemoryFile构造函数中指定的长度。

public void readFile () {
    StringBuffer strBuffer = new StringBuffer();

    try {
        InputStream in = memFile.getInputStream();

        int b;
        int n = 0;
        while ((b = in.read()) != -1) {
            n++;
        }
        Log.d(TAG, String.format("Stream contains %d bytes", n));

        /***************
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String inputString;
        while ((inputString = reader.readLine()) != null) {
            strBuffer.append(inputString + "\n");
        }
        in.close();
        reader.close();

        Log.i(TAG, strBuffer.toString());
        *******************/
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I can't explain why you see output when the read and write are in the same method, but not when they are in separate methods. 我无法解释为什么在同一方法中进行读取和写入时会看到输出,而在不同方法中却看不到输出。 Separate methods worked for me (KitKat on Samsung S3). 单独的方法对我有用(三星S3上的KitKat)。 Now that it is understood that BufferReader can't be used on a MemoryFile, the question of why it worked one way and not the other is not significant. 既然已经了解到BufferReader不能在MemoryFile上使用,为什么它以一种方式而不是另一种方式起作用的问题并不重要。

You may find find these MemoryFile test cases helpful as examples of its use. 您可能会发现这些MemoryFile测试案例可以作为其使用示例的帮助。 I didn't find any other good examples. 我没有找到其他好的例子。

I think the problem is the length of the MemoryFile is not set. 我认为问题是未设置MemoryFile的长度。 memFile = new MemoryFile("MemoryFileTest", 1000000);

Try memFile = new MemoryFile("MemoryFileTest", LENGTH_OF_YOUR_STRING); 试试memFile = new MemoryFile("MemoryFileTest", LENGTH_OF_YOUR_STRING); Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM