簡體   English   中英

刪除Android文件

[英]Delete file android

我正在嘗試刪除文件,但是它不起作用。 我不知道為什么 這是我的函數調用。 String olaola2都有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;
}

有什么問題?

您的myfile.txt沒有存儲它的路徑

應該是這樣的

File sdCard = Environment.getExternalStorageDirectory();   
String path = sdCard.getAbsolutePath() + "/yourfolder/" ;
File f = new File(path +"myfile.txt");
f.delete();

刪除寬度的正確方法:

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

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

最好的。

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個字節。 您將能夠訪問它,但是它的內容可能已經消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM