-2

I know that this is a widely discussed question , but I am really confused with those examples provided on android developers manual . So , I have a "source.txt" in my res/raw folder .For example I want to write 2 lines in it(for ex. Hello\nWorld) and then read them from another activity. Can anyone write the source code for this , please.

SamArmen
  • 9
  • 2

3 Answers3

3

You should replace your .txt file to your extornal or internal storage.And You must give permission for write text from androidManifest.xml

for reading file you can do this

public String readFile(String filePath) {

        String jString = null;
        StringBuilder builder = new StringBuilder();

        File yourFile = new File("/sdcard/" + filepath);
        if (yourFile.exists()) {
            Log.i("file", "file founded");

            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new FileReader(yourFile));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String row = "";

            try {
                while ((row = bufferedReader.readLine()) != null) {

                    builder.append(row + "\n");
                }

                bufferedReader.close();


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            jString = builder.toString();
        }

        else {

            Log.i("FAIL", "FILE NOT FOUND");
        }

        return jString;
    }

for writing file you can use this

public void writetoFile(String filename,String text) {

        File file = new File("/sdcard/" + filename);
        if (!file.exists())
            try {
                file.createNewFile();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        FileWriter fileWriter;
        try {
//you can change second parametre true or false this is about append or clean and write
            fileWriter = new FileWriter(file, false);

            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.append(jsonText);

            bufferedWriter.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
gokhan
  • 627
  • 1
  • 6
  • 16
0

Whatever is in your APK file is read-only, therefore you are unable to write to file stored in res/raw source folder as it is still in your APK. if you want to work on file shipped with your app, you need to copy if to internal storage or SD card from APK first so that would allow you to alter the content.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Every file in your apk is read only, so you need to create the file in your internal storage or SD Card. If you just want to send small amount to of data to second activity, you can send data along with intent or use sharedPreference.

If you really want to read and write data to/from SD card then you need to use FileInputStream and OutputStreamWriter to read/write data to a file. Check this tutorial here to see how it's done. http://www.java-samples.com/showtutorial.php?tutorialid=1523

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107