0

我正在尝试删除一个文件,但它不起作用。我不知道为什么。这是我的函数调用。String ola两者ola2都有,mozi虽然你可以看到我ola在行执行后删除了文件。我期望ola2给我null

    OfflineDataFiles dataFiles = new  OfflineDataFiles();
    dataFiles.writeToFile("mozi", getApplicationContext(), 1);
    String ola =dataFiles.readFromFile(getApplicationContext(), 1);
    boolean answer=dataFiles.DeleteFile(1);
    String ola2 =dataFiles.readFromFile(getApplicationContext(), 1);

删除文件 :

public boolean DeleteFile(int filetype)
{
    File f = new File("myfile.txt");
    boolean ans= f.delete();
     return ans;
}

有什么问题?

4

3 回答 3

1

myfile.txt没有存储它的路径

它应该是这样的

File sdCard = Environment.getExternalStorageDirectory();   
String path = sdCard.getAbsolutePath() + "/yourfolder/" ;
File f = new File(path +"myfile.txt");
f.delete();
于 2013-06-23T17:14:48.030 回答
1

删除宽度的正确方法:

File file = new File(selectedFilePath);
                boolean deleted = file.delete();

selectedFilePath 例如:/sdcard/download/curse.txt

最好的。

于 2013-06-23T17:16:03.017 回答
-1

FileChannel.truncate

public boolean DeleteFile(int filetype) {
    File f = new File("myfile.txt");
    FileChannel outChan = new FileOutputStream(f, true).getChannel();
    outChan.truncate(0);
    outChan.close();
}

From: this 创建对 myfile.txt 的引用,然后将其截断为 0 字节。您将能够访问它,但它的内容可能已经消失了。

于 2013-06-23T17:48:25.407 回答