yasinv
yasinv

Reputation: 83

Error in adding text to existing txt file

Firstly, i know there are same questions in this web site but i couldn't add text to my existing txt file. maybe i miss out something but where ? anyway here are my codes.

i have translate.txt file. it is /raw folder.and When i click the button, the words which are written in the editTexts(w1,w2) must be added to the existing translate.txt file.But it is not working..

public class Add extends Activity {

EditText w1,w2;
Button save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    w1=(EditText)findViewById(R.id.idText1);
    w2=(EditText)findViewById(R.id.idText2);
    save=(Button) findViewById(R.id.idSave);

    save.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            String word1=w1.getText().toString();
            String word2=w2.getText().toString();
            writefile(word1,word2);


        }

    });



}

public void writefile(String word1,String word2)

{
    try
    {
        String finalstring=new String(word1 + " " + word2);
        FileOutputStream fOut = openFileOutput("translate.txt",MODE_APPEND);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(finalstring);
        osw.flush();
        osw.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    } catch(Exception e)
        {
        Toast.makeText(this, "ERROR!!!", Toast.LENGTH_SHORT).show();
        }

}

}

Upvotes: 1

Views: 107

Answers (2)

Ravinder Bhandari
Ravinder Bhandari

Reputation: 2636

Just in case you don't want to store the data in sd card and want to use the previous method the way you was creating a file and stroing data to it was not actually editing the file in res/ raw folder ( because it can not be edited )

but the data you was writing was actually stored in a private file associated with this Context's application package for reading.

hence it was there and the file can be read as follow:

    private void readFile() {
    // TODO Auto-generated method stub
    try {
        FileInputStream fin = openFileInput("translate.txt");
        InputStreamReader isr = new InputStreamReader(fin);
        BufferedReader br = new BufferedReader(isr);
                    String str;
                    StringBuilder str2 = new StringBuilder();
        while ((str = br.readLine()) != null) {
            str2 = str2.append(str);
        }
        isr.close();
        editText.setText(str2.toString());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

you can follow this method incase you dont want to store file in sd card because files in sd crad can be read by anyone.

Upvotes: 1

MDMalik
MDMalik

Reputation: 4001

A) Code to write APPEND file in Android

public void writefile(String word1,String word2)
        try {
                    String path = sdCard.getAbsolutePath() + "/";

        File logFile = new File(path + "translate.txt");
        if (!logFile.exists()) {
            logFile.createNewFile();
        }

        // BufferedWriter for performance, true to set append to file

        FileWriter fw = new FileWriter(logFile, true);
        BufferedWriter buf = new BufferedWriter(fw);


            buf.append(word1 + " " + word2);
            buf.newLine();
            buf.flush();

        }
        buf.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

B) Rule/ Permission AndroidManifest.xml

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

EDIT For user

You cannot write a file to raw folder. Its read-only. Precisely you can't modify anything contained within "Res" folder on the fly.

Check this out, https://stackoverflow.com/a/3374149

Upvotes: 1

Related Questions