Search code examples
androidsd-card

How can I write to a text file


I got reading from a text file line by line working - but I need to write to a text file. I need to write over it really but whatever I try it doesn't seem to work.

I have an existing text file with the value "board1" in it and I need to change that on a button click to board10.

Here is what I'm trying at the moment.

String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();


try {
    FileWriter filenew = new FileWriter(sdcard + "/multi10/default.cfg");
    BufferedWriter bw = new BufferedWriter(filenew);
    bw.write("board12");
    bw.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

if someone could help me I'd really appreciate it!

Thanks


Solution

  • With the little description you have given , I would say, Check the permission in AndroidManifest file for this.

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