【问题标题】:Saving a .txt file dynamically in Android sdcard0 when everytime applicaiion opens每次应用程序打开时在 Android sdcard0 中动态保存 .text 文件
【发布时间】:2015-02-11 09:42:20
【问题描述】:

我收到了一些人的语音记录。我想给他们身份证。我正在尝试在包含新 id 值的 Android sdcard0 中保存 .txt 文件。

我的意思是,我打开新人的应用程序。程序从 txt 文件中读取最后一个 id 值。然后将 +1 值添加到新人的 id。并更新.txt 文件内容。

稍后我关闭应用程序。然后,我再次打开应用程序,读取最后一个 id 值,并保存另一个人 id +1 的声音。我想在每次应用程序打开时动态更新 Android sdcard0 内存中.txt 文件 ID 的内容。

我该怎么做?请帮我。这是我的简单代码。

enter cod private String Load() {
String result = null;;
String FILE_NAME = "counter.txt";

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "Records";
    File file = new File(baseDir, FILE_NAME);

   int counter = 0;
    StringBuilder text = new StringBuilder();

    try {
        FileReader fReader = new FileReader(file);
        BufferedReader bReader = new BufferedReader(fReader);
        //.....??....

        }
        result = String.valueOf(text);
    } catch (IOException e) {
        e.printStackTrace();
    }

return result;

}

【问题讨论】:

  • 如果你只想保存你的计数器值,为什么不使用android sharedPreference?
  • 我该如何做sharedPreference?我是新的 Android Eclipse IDE。请提供更多提示。谢谢。

标签: java android eclipse save txtextcontrol


【解决方案1】:

如果我理解正确,您希望每次打开应用程序时都将 lastid+1 添加到您的文本文件中。您还想在您的 SD 卡上存储和编辑此文件!

有 3 个步骤可以尝试完成此操作:

  1. 从文件中读取
  2. 查找最后添加的 id
  3. 将新 id 写入文本文件
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard, "counter.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String lastLine = "";

    while ((sCurrentLine = br.readLine()) != null) 
    {
        lastLine = sCurrentLine;
    }

    br.close();
    //Parse the string into an actual int.
    int lastId = Integer.parseInt(lastLine);


    //This will allow you to write to the file
    //the boolean true tell the FileOutputStream to append
    //instead of replacing the exisiting text
    outStream = new FileOutputStream(file, true);
    outStreamWriter = new OutputStreamWriter(outStream); 
    int newId = lastId + 1;

    //Write the newId at the bottom of the file!
    outStreamWriter.append(Integer.toString(newId));
    outStreamWriter.flush();
    outStreamWriter.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

写入外部存储(如 SD 卡)需要您的 Android 清单中的特殊权限,只需添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

应该这样做!

有关参考资料,请查看以下链接:

How can I read a text file in Android?

how to read last line in a text file using java

android saving to SD card as text file

append text to the end of the file

【讨论】:

    【解决方案2】:

    如果你想要的只是持久的原始数据,例如保存/加载一个 int 值, 你应该使用android共享偏好机制: shared preference example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多