File类使用
簡(jiǎn)介
File類的實(shí)例代表了一個(gè)文件或者一個(gè)目錄,通過API可以獲取這個(gè)對(duì)象的相關(guān)信息。
File類代表的文件或者目錄可以真實(shí)存在,也可以是不存在的,可以使用File.exists()來判斷。
在Windows系統(tǒng)中,絕對(duì)路徑是以盤符開頭的,例如 "E:\\java\Hello.java"
而在類Unix系統(tǒng)中,絕對(duì)路徑以"/"開頭,例如 "/home/bob/java/Hello.java"
相對(duì)路徑以某個(gè)目錄或者文件名開頭 ,例如 ?"images\a.jpg",但是相對(duì)路徑單獨(dú)存在是沒有意義的,還需要指定他的父目錄。
例如 "E:\java\images\a.jpg" ?和 ?"E:\C++\images\a.jpg" 是2個(gè)不同的文件。
?
構(gòu)造函數(shù)
File(String pathname)
通過將給定路徑名字符串來創(chuàng)建一個(gè)新 File 實(shí)例。
File(String parent, String child)
根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
File(File parent, String child)
根據(jù) parent 抽象路徑名和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
File(URI uri)
通過將給定的 file: URI 來創(chuàng)建一個(gè)新的 File 實(shí)例。
?
靜態(tài)字段
public static String seperator 系統(tǒng)文件路徑分隔符 window下是"\" linux 是 "/" public static char seperatorChar 字符類型,同上。 public static String pathSeperator 系統(tǒng)環(huán)境變量中的分隔符。 widows下是 ";" linux下是 ":" public static char pathSeperator 字符類型,同上。?
實(shí)例方法
下面是比較常用的API,更多的參看JDK文檔。注意:有些API是只針對(duì)目錄,而有些只針對(duì)文件才有意義。
boolean exists() 這個(gè)文件或者目錄在機(jī)器上是否真實(shí)存在。boolean canExecute()是否可被程序執(zhí)行。boolean canRead()是否可以被程序讀取文件中的數(shù)據(jù)。boolean canWrite()是否可以被程序?qū)懭霐?shù)據(jù)到文件。boolean isFile()是一個(gè)文件嗎?boolean isDirectory()是一個(gè)目錄嗎?boolean isAbsolute()這個(gè)目錄或者文件是以絕對(duì)路徑表示的嗎?long length()返回文件的大小,即字節(jié)數(shù)。long lastModified() 返回文件最近一次被修改的時(shí)間的long類型時(shí)間表示。可以通過 Date類構(gòu)造轉(zhuǎn)換為年月日格式的時(shí)間。通過這個(gè)參數(shù)用來比較,來確認(rèn)文件是否被修改過。String getName()返回這個(gè)文件或者目錄的名稱。String getParent()返回父目錄String[] list()如果File對(duì)象代表的是目錄,則返回這個(gè)目錄下所有內(nèi)容(文件是文件名,目錄是目錄名)的String表示的數(shù)組。?
代碼演示
import java.io.File; import java.util.Date;public class AllDemo {public static void main(String[] args){analyseFile("E:\\a.txt");System.out.println("--------------------");analyseFile("E:\\Java");}public static void analyseFile(String path){File file = new File(path);if (file.exists()){if(file.isFile()){System.out.println("name:"+file.getName());System.out.println("isFile?:"+file.isFile());System.out.println("length:"+file.length());System.out.println("canRead?:"+file.canRead());System.out.println("lastModified:"+new Date(file.lastModified()).toLocaleString());System.out.println("absolute path:"+file.getAbsolutePath());}else if(file.isDirectory()) {System.out.println("name:"+file.getName());System.out.println("isDrictory?:"+file.isDirectory());System.out.println("length:"+file.length());System.out.println("canRead?:"+file.canRead());System.out.println("lastModified:"+new Date(file.lastModified()).toLocaleString());System.out.println("absolute path:"+file.getAbsolutePath());}}}}輸出:
name:a.txt
isFile?:true
length:10
canRead?:true
lastModified:2017-9-10 23:24:25
absolute path:E:\a.txt
--------------------
name:Java
isDrictory?:true
length:4096
canRead?:true
lastModified:2017-9-7 21:19:03
absolute path:E:\Java
?
轉(zhuǎn)載于:https://www.cnblogs.com/lulipro/p/7503066.html
總結(jié)
- 上一篇: BZOJ 1609 Usaco Eati
- 下一篇: IdentityServer4(7)-