Writing to the internal private storage in Android

后端 未结 6 2267
粉色の甜心
粉色の甜心 2020-12-06 01:44

I am trying to save some data from my app in a simple text file on the internal private storage area (app user preferences). I have read through many questions on here (Stac

6条回答
  •  再見小時候
    2020-12-06 02:35

    As @bianca says, you're better not using a file path. But instead, use only the filename to create a File. Something like this:

    public static void saveTextToFile(Context context, String filename, String content) {
      try {
         FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
         fos.write(content.getBytes());
         fos.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
    }
    

    And to get the file, you can use:

    File file = new File(context.getFilesDir(), filename);
    

    Read more: Saving Files.

提交回复
热议问题