0

所以在我制作的这个应用程序中,用户创建了一个项目,当他们保存时,帧数会保存到 SD 卡上的 numberFrames.txt 中。然后我在另一个类中检索文件。唯一的问题是,当我在运行此代码后向屏幕显示 nFrames 时,nFrames = 50。我对 nFrames 所做的唯一初始化是在位于 onCreate() 中的代码正上方归零。

File sdcardLocal =  Environment.getExternalStorageDirectory();
                 File dir = new File (sdcardLocal.getAbsolutePath() + "/Flipbook/"+customizeDialog.getTitle()+"/");
                 dir.mkdirs();
                 File fileNum = new File(dir, "numberFrames.txt");
                 FileWriter myFileWriter = null;
        try {
         myFileWriter = new FileWriter(fileNum);
        } catch (IOException e) {
         e.printStackTrace();
        }
                 BufferedWriter out = new BufferedWriter(myFileWriter); 
                          try {
                           String text = bitmaps.size()+"";
                           out.write(text);
         out.close();
        } catch (IOException e) {
         e.printStackTrace();
        }

我像这样检索文件。我不知道 nFrames 的这个“50”值是从哪里来的,因为周围没有循环,而且我确信保存的特定项目只有 3 帧。为什么是这样?

FileInputStream is = null;
             BufferedInputStream bis = null;
             try {
                 is = new FileInputStream(new File(mFolderDialog.getPath()+"/numberFrames.txt"));
                 bis = new BufferedInputStream(is);
                 nFrames = bis.read();
             }catch (IOException e) {
     e.printStackTrace();
    }
4

1 回答 1

2

您正在写出一个字符串,然后将第一个字节作为整数读取。50 是 '2' 字符的 ASCII 码。

您可以使用 BufferedReader.readLine 将文件的整个第一行作为字符串读取,然后使用 Integer.parseInt 将其转换为整数。

此外,我会仔细研究您的应用程序的工作流程。您没有提供太多信息,但是将具有单个整数值的文件保存到 sdcard 有一定的“气味”:)。您是否考虑过使用数据库,或者可能将文本文件存储在应用程序的目录中?

于 2010-12-30T03:04:36.937 回答