0

Does anyone know how to write to a text file in Android using a resource like:

R.raw.my_text_file

I'm just looking for something clean and simple. I've Googled around but the examples I found didn't work. I tried to code using the Android docs but couldn't wrap my head around it...

Thanks

EDIT:

I've used the Android docs to create this code. The logs print out "1" and "9" and the code skips everything in between and does nothing:

    try {
        String filename = "res/raw/my_text_file.txt";
        String string;

        Log.v(TAG, "1");
        FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
        Log.v(TAG, "2");

        for (int i = 0; i < list.size(); i++) {
            Log.v(TAG, "3");
            try {
                Log.v(TAG, "4");
                string = i + " - " + list.get(i);
                fos.write(string.getBytes());
            } catch (Exception e) {
                Log.v(TAG, "5");
            }
            Log.v(TAG, "6");
        }
        Log.v(TAG, "7");
        fos.close();
        Log.v(TAG, "8");
    } catch (Exception e) {

    }
    Log.v(TAG, "9");

2 Answers2

13

Does anyone know how to write to a text file in Android using a resource

Resources are read-only at runtime and cannot be written to.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • In that case, would it work if I were to write to a text file the normal Java way? –  May 04 '12 at 22:47
  • 1
    @ThreaT: You are certainly welcome to use standard Java I/O to write to internal or external storage. http://developer.android.com/guide/topics/data/data-storage.html – CommonsWare May 04 '12 at 22:49
  • Also, how can I obtain the file path of a resource? –  May 04 '12 at 22:50
  • @ThreaT: There is no "file path of a resource". A resource is not a file at runtime. – CommonsWare May 04 '12 at 23:28
  • Oh... well I updated my code above, any ideas why it doesn't write to the file? –  May 04 '12 at 23:30
  • @ThreaT: Because `openFileOutput()` does not work with a path. It has to be a simple filename -- no slashes. You would have known this had you done something with the exception that was raised (e.g., use `Log.e()` to log it to LogCat), instead of ignoring it. – CommonsWare May 04 '12 at 23:35
  • If I change it to: String filename = "hello_file.txt"; then will it create that file before writing to it? If so, where will it create it to? –  May 04 '12 at 23:38
  • @ThreaT: It will create the file on the phone. You will not be able to access the file on production hardware except via your app (e.g., `openFileInput()`. On an emulator, you will be able to see the file at `/data/data/your.package.goes.here/files/`, where `your.package.goes.here` is replaced by whatever the `package` is on your `` element in your `AndroidManifest.xml` file. – CommonsWare May 04 '12 at 23:39
  • I understand. So: String filename = "hello_file.txt"; - will create "hello_file.txt" in com.my.package/files on the emulator or mobile device that's currently running the application? –  May 04 '12 at 23:45
0

As @CommonsWare said you can't write on resources but you could use internal storage using openFileOutput and openFileInput and BufferedReaders and BufferedWriters. You can check it here:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

But here's a sample:

BufferedReader BR_intFile = new BufferedReader(new InputStreamReader(openFileInput("file")));

To read and a similar code to write.

Moises Jimenez
  • 1,962
  • 3
  • 21
  • 43
  • Thanks. I got it to read, I just can't get it to write. I pasted my code under the "EDIT" section in my question above. –  May 04 '12 at 23:16
  • Try saving the file somewhere other than res/raw – Moises Jimenez May 04 '12 at 23:22
  • Ok now when I made it: String filename = "hello_file"; then it goes through and logs all of the numbers and runs all the code, but the file "hello_file" doesn't exist anywhere in my workspace - so I don't think it created it. Am I supposed to create it as well? If so, do you know how? –  May 04 '12 at 23:27
  • If you want to check its contents read from it from within the app as I believe the file is private to the application and you can't access it from out of it. – Moises Jimenez May 04 '12 at 23:32
  • Isn't there a way to write to a file that's situated outside of the application? –  May 04 '12 at 23:33
  • You'd have to use external storage. Checking whether or not the media is available and so on. Check it out here http://developer.android.com/guide/topics/data/data-storage.html#filesExternal – Moises Jimenez May 04 '12 at 23:36
  • @ThreaT: "doesn't exist anywhere in my workspace" -- you are writing a file *on the phone*. That phone might be thousands of kilometers from your current location; it assuredly has no access to your workspace. – CommonsWare May 04 '12 at 23:37