Java File

一、FileOutPutStream在c盤等一級目錄下是能夠建立文件的,如: new FileOutputStream("c:\\kk.txt");可是在c\\test等就建立不了,File也是同樣,爲何呢?直接去看jdk,大概是爲了防止隨意建立文件對磁盤攻擊吧。php

   所以「java.io.FileNotFoundException 系統找不到指定的路徑"的問題會常常出如今咱們的編程中,緣由可能有三:(1)文件名亂碼。你工程的編碼是 utf-8仍是 gbk,若是是gbk那麼當心文件名若是有中文,會亂碼。java

(2)沒有建立文件夾,在沒有文件夾狀況下,建立文件將報錯.解決方案爲先建立文件夾,具體代碼見下面。編程

(3)有文件夾,但文件所在位置無文件夾,即文件路徑太深,超過二級。編碼

//建立目錄code

String dir="F:\\TestIO";utf-8

File file = new File(dir);get

if (!file.exists()) {io

file.mkdirs();test

}亂碼

//建立文件

File file2 = new File(dir,"temp.txt");

if (file2.exists()) {

file2.delete();

}else {

file2.createNewFile();

}

二、報錯:java.io.IOException: open failed: EBUSY (Device or resource busy)

報錯:java.io.FileNotFoundException: open failed: EBUSY (Device or resource busy)

at libcore.io.IoBridge.open

解決辦法:

把要刪除的文件更名而後再刪除,下次下載時,就算沒刪掉,文件名也改了,因此第二次就能下載了。

刪除文件代碼:

   public void deleteSDCardFolder(File dir) {
       File to = new File(dir.getAbsolutePath() + System.currentTimeMillis());
       dir.renameTo(to);
       if (to.isDirectory()) {
           String[] children = to.list();
           for (int i = 0; i < children.length; i++) {
               File temp = new File(to, children[i]);
               if (temp.isDirectory()) {
                   deleteSDCardFolder(temp);
               } else {
                   boolean b = temp.delete();
                   if (b == false) {
                       Log.d("deleteSDCardFolder", "DELETE FAIL");
                   }
               }
           }
           to.delete();
       }

}

參考地址:http://www.cfanz.cn/index.php?c=article&a=read&id=141809

http://stackoverflow.com/questions/11539657/open-failed-ebusy-device-or-resource-busy

相關文章
相關標籤/搜索