简体   繁体   English

删除内部保存文件-Android

[英]Delete an internal save file - Android

How do I delete an internal save file dynamically in an Android application? 如何在Android应用程序中动态删除内部保存文件? I saved it in the default directory so I don't know the exact file path. 我将其保存在默认目录中,所以我不知道确切的文件路径。 Here is the code I used to save my files if that helps any: 这是我用来保存文件的代码(如果有帮助的话):

public void saveAssignments(){ 公共无效的saveAssignments(){

    String saveData = "";
    String FILENAME = name.replaceAll(" ", "") + ".txt";
    //Context context = getApplicationContext();
    Context context = getActivity();
    FileOutputStream fos;

    for(int i = 0; i < allEds.size(); i++){
            saveData = saveData + allEds.get(i).getText().toString() + ", ";
    }


    try{
        fos = context.openFileOutput( FILENAME, Context.MODE_PRIVATE );
        try{
            fos.write(saveData.getBytes());
            fos.close();
            //Toast.makeText(context, "Saved as " + FILENAME, 5000).show(); //popup message
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

You could use method 你可以使用方法

deleteFile(String filename) 

on your context-object. 在您的上下文对象上。

http://developer.android.com/reference/android/content/Context.html#deleteFile%28java.lang.String%29 http://developer.android.com/reference/android/content/Context.html#deleteFile%28java.lang.String%29

Furthermore you could use 此外,您可以使用

 String[] fileList ()

to query your files. 查询您的文件。

http://developer.android.com/reference/android/content/Context.html#fileList%28%29 http://developer.android.com/reference/android/content/Context.html#fileList%28%29

You can delete file created by using openFileOutput method as: 您可以通过以下方式删除使用openFileOutput方法创建的文件:

File file=new File(context.getFilesDir().getAbsolutePath()+"/"+FILENAME);
if(file.exists())file.delete();

You should try 你应该试试

getFilesDir() from context to return path or refer to here . context获取getFilesDir()返回路径或参考此处 Then you can delete it. 然后您可以将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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