File類簡單使用

2021-09-24 23:13:02 字數 3403 閱讀 8805

file(string pathname):根據乙個路徑得到file物件

file(string parent,string child):根據乙個目錄和乙個子檔案/目錄得到file物件

file(file parent,string child):根據乙個父file物件和乙個子檔案/目錄得到file物件

file file = new file("c:\\users\\durian\\desktop\\新建文字文件 (2).txt");

boolean exists = file.exists();

system.out.println(exists);

system.out.println(new file("2.txt").exists());

string parent="c:\\users\\durian\\desktop";

string child="新建文字文件 (2).txt";

file file = new file(parent,child);

system.out.println(file.exists());

file parent=new file("c:\\users\\durian\\desktop");

string child="新建文字文件 (2).txt";

file file = new file(parent,child);

system.out.println(file.exists());

建立乙個新的檔案或者是乙個新的資料夾:

file file = new file("11.txt");

//建立乙個新檔案,返回的是布林值,如果有返回false,如果沒有建立乙個,返回true

system.out.println(file.createnewfile());

file file2 = new file("11");

//建立乙個新資料夾,返回的是布林值,如果有返回false,如果沒有建立乙個,返回true

system.out.println(file2.mkdir());

file file3 = new file("11\\22");

//建立多級新資料夾,返回的是布林值,如果有返回false,如果沒有建立乙個,返回true

system.out.println(file3.mkdirs());

重新命名和刪除

file file1 = new file("1.txt");

file file2 = new file("222.txt");

// 如果路徑相同就是改名,如果路徑不同就是剪下

system.out.println(file1.renameto(file2));

file file = new file("11.txt");

// 刪除檔案或者是資料夾,如果刪除乙個資料夾,資料夾必須是空的

system.out.println(file.delete());

file file2 = new file("11");

system.out.println(file2.delete());

file的判斷能力

file file = new file("11");

//判斷是否是目錄

system.out.println(file.isdirectory());

file file2 = new file("222.txt");

//判斷是不是檔案

system.out.println(file2.isfile());

file file3 = new file("222.txt");

//判斷檔案存不存在

system.out.println(file3.exists());

file file4 = new file("222.txt");

file4.setreadable(false);

//判斷檔案是否可讀

//window系統認為所有的檔案都是可以讀的

system.out.println(file4.canread());

file file5 = new file("222.txt");

file5.setwritable(false);

//判斷檔案是不是可寫

//window系統可以設定為不可寫

system.out.println(file5.canwrite());

file file6 = new file("222.txt");

//判斷檔案是不是隱藏的

system.out.println(file6.ishidden());

獲取檔案路徑和最近的修改時間

file file = new file("222.txt");

//獲取絕對路徑

system.out.println(file.getabsolutepath());

//獲取構造方法裡面傳入的路徑

system.out.println(file.getpath());

//獲取檔案或者是資料夾的名字

system.out.println(file.getname());

system.out.println(file.length());

//獲取最後一次修改的時間值(毫秒值)

date date = new date(file.lastmodified());

******dateformat sdf = new ******dateformat("yyyy年mm月dd日 hh:mm:ss");

system.out.println(sdf.format(date));

file file2 = new file("c:\\users\\durian\\desktop\\中專");

//獲取路徑下每乙個檔案的名稱

string list = file2.list();

for (string string : list)

//獲取每個檔案的file物件

file listfiles = file2.listfiles();

for (file file3 : listfiles)

檔名稱過濾

file file = new file("e:\\");

string list = file.list(new filenamefilter()

});for (string string : list)

File 類的簡單使用

分隔符 test public void test01 檔案的相關屬性 test public void test02 建立檔案 test public void test03 判斷該檔案是否存在 boolean flag false if file.exists catch ioexception...

File類的使用

file 表示磁碟或者網路的某個檔案或者資料夾,物件導向 磁碟上的檔案或者網路上的檔案,資料夾都是物件 file的物件用來封裝乙個檔案或者資料夾。建立file物件,封裝檔案 file fil3 newfile d test2 2.txt 判斷dir物件封裝的是否是乙個檔案 boolean resul...

File類的使用

j a.io.file類表示檔案或目錄,只用於表示檔案或目錄得資訊,不能用於檔案的訪問。常用的api 1.建立file物件 file file new file string path 注意 file.seperater 獲取系統分隔符,如 2.boolean file.exists 是否存在.3....