如何导出我的sharedPreferences文件

时间:2014-09-02 02:07:35

标签: android sharedpreferences fileinputstream

您好我的应用程序我想将SharedPrefernces文件备份到我的设备存储。我尝试导出它,就像我导出我的数据库文件但它给了我一个错误。" java.io.filenotfoundexception / data / data / com.example.myapp / shared_prefs / myPref:open failed:ENOENT (没有这样的文件或难治) 这是我的代码:

@SuppressLint("SdCardPath")
private void exportPref() throws IOException {
      // Open your local db as the input stream
    try {
        String inFileName = "/data/data/com.bibas.workclocks/shared_pref/"+MySharedPreferences.MY_TEMP;
        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);

        String outFileName = Environment.getExternalStorageDirectory()+ "/MyPrefs";

        // Open the empty db as the output stream
        OutputStream output = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        // Close the streams
        output.flush();
        output.close();
        fis.close();
    } catch (Exception e) {
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG)
                .show();
    }
    Toast.makeText(context,
            "'",
            Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:1)

有关保存SharedPreferences的实例,请参阅此post