0

I'm getting voice records from some people. I want to give them id's. I'm trying to save a .txt file in Android sdcard0 which contains new id value.

I mean, I open the application for new person. Program reads last id value from txt file. And then add +1 value to the new person's id. And update the .txt file content.

Later I close the application. And then, I open the application again, and read the last id value, and save the another person's voice with person id +1. I want to update .txt file id's content in Android sdcard0 memory with dynamically every time the application opens.

How can I do that? Please help me. Here is my simple code.

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;

}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gianna
  • 11
  • 6

2 Answers2

1

if I understand correctly you want to add lastid+1 to your textfile every time you open the application. And you want to store and edit this file on your Sd card as well!

There are 3 steps to try and accomplish this:

  1. Read from the file
  2. Find the last added id
  3. Write the new id to the text file
//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
}

Writing to an external storage like a SD card requires a special permission in your Android Manifest, just add

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

And that should do it!

For Reference material have a look at these links:

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

Community
  • 1
  • 1
Bart de Ruijter
  • 917
  • 10
  • 21
0

if all you want is persistent primitive data such as save/load an int value, you should use android shared preferences mechanism: shared preference example

Community
  • 1
  • 1
chipopo
  • 326
  • 2
  • 9