java檔案建立 刪除 讀取 寫入操作大全

2021-08-21 19:13:46 字數 1866 閱讀 3306

一.獲得控制台使用者輸入的資訊

public string getinputmessage() throws ioexception... catch (filenotfoundexception e) ...

}

2.利用stringbuffer寫檔案

public void stringbufferdemo() throws ioexception......        

out.close();

}

該方法可以設定使用何種編碼,有效解決中文問題。

四.檔案重新命名

public void renamefile(string path,string oldname,string newname)...

}

}

五.轉移檔案目錄

轉移檔案目錄不等同於複製檔案,複製檔案是複製後兩個目錄都存在該檔案,而轉移檔案目錄則是轉移後,只有新目錄中存在該檔案。

public void changedirectory(string filename,string oldpath,string newpath,boolean cover)...

else...

}

}

六.讀檔案

1.利用fileinputstream讀取檔案

public string fileinputstreamdemo(string path) throws ioexception...

return sb.tostring();

}

2.利用bufferedreader讀取

在io操作,利用bufferedreader和bufferedwriter效率會更高一點

public string bufferedreaderdemo(string path) throws ioexception...

return sb.tostring();

}

3.利用dom4j讀取xml檔案

public document readxml(string path) throws documentexception, ioexception...
七.建立檔案(資料夾)

1.建立資料夾

public void createdir(string path)...
2.建立新檔案

public void createfile(string path,string filename) throws ioexception...
八.刪除檔案(目錄)

1.刪除檔案

public void delfile(string path,string filename)...
2.刪除目錄

要利用file類的delete()方法刪除目錄時,必須保證該目錄下沒有檔案或者子目錄,否則刪除失敗,因此在實際應用中,我們要刪除目錄,必須利用遞迴刪除該目錄下的所有子目錄和檔案,然後再刪除該目錄。  

public void deldir(string path)...{

file dir=new file(path);

if(dir.exists())...{

file tmp=dir.listfiles();

for(int i=0;i

java檔案建立 刪除 讀取 寫入操作大全

一.獲得控制台使用者輸入的資訊 public string getinputmessage throws ioexception.可以返回使用者輸入的資訊,不足之處在於不支援中文輸入,有待進一步改進。二.複製檔案 1.以檔案流的方式複製檔案 public void copyfile string s...

建立 寫入 讀取 刪除 Plist 檔案中資料

建立plist檔案 找到第乙個documents資料夾所在的路徑 nsstring cachepatch nssearchpathfordirectoriesindomains nscachesdirectory,nsuserdomainmask,yes 0 把procucts檔案加入 produc...

iPhone開發之檔案建立 刪除 讀取 寫入

建立與刪除 建立檔案管理器 nsfilemanager filemanager nsfilemanager defaultmanager 獲取路徑 引數nsdocumentdirectory要獲取那種路徑 nsarray paths nssearchpathfordirectoriesindomai...