简体   繁体   中英

Open text file and write to it android

My app is structured by an edit text and a button that sends the words to a file (script) located into /system/etc/init.d and if the file is not there it creates a new one. When I rewrite something in the edit text I wanna append the text in same file and not recreate a new one. Hope you can give me some example code and help me

您必须使用sth这样的。

FileOutputStream fOut = openFileOutput("script", MODE_APPEND);

Hope this works for you.

    File externalStorageDir = Environment.getExternalStorageDirectory();
    File myFile = new File(externalStorageDir , "myfile.txt");

    if(myFile.exists())
    {
       try
       {
  FileOutputStream fOut = new FileOutputStream(myFile);
           OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
           myOutWriter.append("test");
           myOutWriter.close();
           fOut.close();
        } catch(Exception e)
        {

        }
    }
    else
    {
        myFile.createNewFile();
    }

just set permission like this

android.permission.WRITE_EXTERNAL_STORAGE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM