File類的用法

2021-10-02 01:16:54 字數 1038 閱讀 1320

使用file類建立乙個檔案的放法

1.file file1=new file(string lujingming);

2.file file2=new file(string parentpath,string childpath);

3.file file3=new file(file1,lujingming);  //這裡面的第乙個引數是乙個file物件

比如:

file file1=new file(session.getservletcontext().getrealpath("img/productdetail"));

file file=new file(file1,path);

這樣並不會建立乙個真正的資料夾,只是會建立乙個file的物件而已

file.createnewfile();

file.mkdir(); 建立單層目錄資料夾

file.mkdirs(); 建立多層目錄資料夾

new file("/tmp/one/two/three").mkdirs();

執行後, 會建立tmp/one/two/three四級目錄

new file("/tmp/one/two/three").mkdir();

則不會建立任何目錄, 因為找不到/tmp/one/two目錄, 結果返回false

file file = new file("d:/1/1.txt");

system.out.println(file.createnewfile());//注意:檔案所在路徑(在這裡的路徑指:d:/1)必須存在  才能建立檔案(1.txt)!!

//建立資料夾:

file file2 = new file("d:/1/新建資料夾");

system.out.println(file2.mkdir());//如果沒有父路徑,不會報錯,但不會建立資料夾

如果父路徑不存在,會自動先建立路徑所需的資料夾,就是一串路徑全都給你建立了

刪除檔案直接就是 file.delete();

Java中File類的用法

檔案和目錄路徑名的抽象表現形式 方法名方法解釋 canread 測試應用程式是否可以讀取此抽象路徑名表示的檔案 canwrite 測試應用程式是否可以修改此抽象路徑名表示的檔案 compareto file pathname 按字母順序比較兩個抽象路徑名 canexecute 測試應用程式是否可以執...

File類的學習

file類直接管理檔案 目錄,file對像代表乙個檔案 目錄 file物件主要用來獲取檔案本身的一些資訊,不涉及對檔案的讀寫操作 方法 說明file file parent,string child 根據parent 抽象路徑和child路徑名字串建立乙個新file例項 file string pa...

File類的功能

file類 表示檔案或者目錄的路徑的抽象表現形式.io流就是對檔案進行操作的 public file string pathname 表示pathname的抽象路徑表現的形式 開發中使用這種方式 例句 file file new file e demo a.txt 表示 e盤下的demo資料夾中的a...