我试图删除一个文件,但它不起作用。我也不知道原因。这是我的函数调用。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;
}
有什么问题吗?
发布于 2013-06-23 17:14:48
您的myfile.txt
没有存储它的路径
应该有点像
File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/yourfolder/" ;
File f = new File(path +"myfile.txt");
f.delete();
发布于 2013-06-23 17:16:03
删除宽度正确的方法:
File file = new File(selectedFilePath);
boolean deleted = file.delete();
例如,selectedFilePath : /sdcard/download/curse.txt
最棒的。
发布于 2013-06-23 17:48:25
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();
}
出发地:这创建对myfile.txt的引用,而不是将其中继到0字节。您将能够访问它,但可能它的内容已经消失。
https://stackoverflow.com/questions/17263177
复制相似问题