0

I created a raw folder inside res (res/raw) and I also created my_text_file.txt file.
Now I want to write something in this file.
I wrote some code but I cannot write (for example) a simple string.

This is my code.

If anyone knows what is wrong in my code, please help me

try {
                FileOutputStream fos = openFileOutput("my_text_file.txt",
                        Context.MODE_PRIVATE);
                OutputStreamWriter osw = new OutputStreamWriter(fos);
                osw.write("17");
                osw.flush();
                osw.close();
            } catch (java.io.IOException e) {
                // do something if an IOException occurs.
            }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You cannot write into resource files ... – gunar Jan 30 '14 at 19:23
  • possible duplicate of [Android and ObjectOutputStream to resource file](http://stackoverflow.com/questions/8747713/android-and-objectoutputstream-to-resource-file) – kabuko Jan 30 '14 at 19:28

2 Answers2

0

You can read your file but not alter the file on the resources folder. What you can do is to save the file in the external storage then start to alter the file.

Dont forget to set the permission:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • thanks i also use this permission.my problem is.first time i write code whitch can to create txt file in sdcard but i want to this file would show only me.meybe problem solution is to hide this txt file on sdcard but for my option it is not possible – user3233500 Jan 30 '14 at 19:32
  • Can you explain why? Maybe you can try another solution, you can set a dot before your folder's name ".myapp/text.txt" , this way the user can't access your content from Android or Unix based pcs – GhostDerfel Jan 30 '14 at 19:38
  • If you want the file to show only for you then you could store it in your app's folder, for example: `String absoluteFilePath = getApplicationContext().getFilesDir().getPath() + "/" + fileName; FileOutputStream fileOut = new FileOutputStream(absoluteFilePath);` that way only your app will be able to access it. – Piovezan Jan 30 '14 at 19:40
  • thanks .this is s right syntacs ? /sdcard/.json.txt – user3233500 Jan 30 '14 at 19:41
  • Yes , can be.You know you dont need to set the ".txt" in the end of your file right? that way will be easy to your user identify the file – GhostDerfel Jan 30 '14 at 19:44
0

You shouldn't write to resource files. It exist to store data that you put here before compilation. If you want to save some information in file, during runtime you can do something like this:

public static void writeToFile(String fileName, String encoding, String text) {
    Writer writer = null;

    try {
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), encoding));
        writer.write(text);
    } catch (IOException ex) {
        Log.e(TAG, "", ex);
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}

To find path to SD card you can use this method:

Environment.getExternalStorageState()

So you can use this method like this:

writeToFile(Environment.getExternalStorageState() + "/" + "my_text_file.txt", "UTF-8", "my_text");

And don't forgot to set permission

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

UPD: You shouldn't use SD card to store some secure information! More information here.

To write data that will be available only for you application, use this code:

public static void writeToInternalFile(String fileName, String text) {
    FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
    fos.write(text.getBytes());
    fos.close();
}

To read from this file:

public static String readFromInternalFile(String fileName) {
    FileInputStream fis = openFileInput(, Context.MODE_PRIVATE);
    StringBuilder sb = new StringBuilder();
    int ch;
    while((ch = fis.read()) != -1){
        sb .append((char)ch);
    }
    return sb.toString();
}
Divers
  • 9,531
  • 7
  • 45
  • 88