如何将字符串存储到Android中的文件

时间:2019-06-04 08:10:43

标签: java android

我正在尝试将字符串存储到应存储在设备上的文件中

我有以下代码将String存储到一个名为exportstring的变量中:

final JSONArray jsonArray1 = jsonArray;
JSONObject export = jsonArray1.getJSONObject(index);
String exportString = export.toString();
writeToFile(exportString);

然后我有了一个名为writeToFile

的函数
private void writeToFile(String content) {
    try {
        File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");

        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file);
        writer.append(content);
        writer.flush();
        writer.close();
    } catch (IOException e) {
    }
}

当我运行它时,出现一条消息,提示“文件已保存”

但是当我打开文件管理器时,在任何地方都看不到它。

文件保存在哪里?

还是我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

要将文件存储在“下载”文件夹中,请更改:

File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");

收件人:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test.txt");

更新:之前,尝试保存文件之前,请确保您具有必需的权限,在这种情况下,是写入存储的权限。下面是一个简短的示例,说明了如何做到这一点:

protected boolean checkPermission() {
        int result1 = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result1 == PackageManager.PERMISSION_GRANTED  ) {
            return true;
        } else {
            return false;
        }
    }

protected void requestPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(this, "Write External Storage permission allows us to do store files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
            }
        }
    }


@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 100:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("permissionCheck", "Permission Granted, do what you want to do when user grants the permission here!!!");

                } else {
                    Log.d("permissionCheck", "Permission Denied, do what you want to do when user denies the permission here...");
                }      
                break;
        }
    }

将上面的代码包括在您的活动(或您当前从事的活动之前的任何活动)中,并之前尝试将字符串存储到文件中,请执行以下操作:

if(!checkPermission()){
   Log.d("permissionCheck", "Need to get the permissions");
   requestPermission();
}else{
   Log.d("permissionCheck", "Already have the permissions");
}

要查看logcat中的日志:

enter image description here