android replace string by another string in file on sdcard
<p>I've created <strong>Test.txt</strong> on sdcard and write string "test example" on it.<br>
after that, I replace string "test" by "etc" in Test.txt.<br>
this is my code :</p>

<pre><code>String origin_str, old_str , new_str;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_t2);

            origin_str = "test example";
            old_str = "test";
            new_str = "etc";

            Button bt_create2 = (Button)findViewById(R.id.bt_createfileT2);
            bt_create2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
                        if (!newFolder.exists()) {
                            newFolder.mkdir();
                        }

                        File file = new File(newFolder, "Test" + ".txt");
                        if (!file.exists()) {
                            file.createNewFile();
                            FileOutputStream fOut = new FileOutputStream(file);
                            OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
                            myOutWriter.append(origin_str);
                            myOutWriter.close();
                            fOut.close();
                        }
                    } catch (Exception e) {
                        System.out.println("e: " + e);
                    }
                }
            });

            Button bt_replacefileT2 = (Button)findViewById(R.id.bt_replacefileT2);
            bt_replacefileT2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        File file = new File(Environment.getExternalStorageDirectory() + "/TestFolder/Test.txt");
                        FileInputStream in = new FileInputStream(file);
                        int len = 0;
                        byte[] data1 = new byte[1024];
                        while ( -1 != (len = in.read(data1)) ){
                            if(new String(data1, 0, len).contains(old_str)){
                                String s = "";
                                s = s.replace(old_str, new_str);
                            }
                        }

                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }

                }
            });
</code></pre>

<p>with this code, it was create Test.txt on sdcard and write "test example" on it.<br>
but when replace string "test" by "etc", it not working.<br>
how to fix it?</p>