淺談JAVA檔案操作(File類)

來源:互聯網
上載者:User

淺談JAVA檔案操作(File類)

 

使用者介面和作業系統使用與系統相關的路徑名字串來命名檔案和目錄。此類呈現分層路徑名的一個抽象的、與系統無關的視圖。抽象路徑名有兩個組件:
一個可選的與系統有關的前置詞字元串,比如盤符,"/" 表示 UNIX 中的根目錄,"////" 表示 Microsoft Windows UNC 路徑名,以及零個或更多字串名稱的序列。
除了最後一個,抽象路徑名中的每個名稱代表一個目錄;最後一個名稱既可以代表目錄,也可以代表檔案。空的抽象路徑名沒有首碼和名稱序列。

路徑名字串與抽象路徑名之間的轉換與系統有關。將抽象路徑名轉換為路徑名字串時,每個名稱與下一個名稱之間由單個預設分隔符號字元 隔開。預設名稱分隔字元由系統屬性 file.separator 定義,也可以從此類的公用靜態欄位 separator 和 separatorChar 中得到。將路徑名字串轉換為抽象路徑名時,可以使用預設名稱分隔字元或者受基礎系統支援的其他任何名稱分隔字元來分隔其中的名稱。

無論是抽象路徑名還是字串路徑名,都可以是絕對 路徑名或相對 路徑名。絕對路徑名是完整的路徑名,不需要任何其他資訊就可以定位自身表示的檔案。相反,相對路徑名必須使用來自其他路徑名的資訊進行解釋。預設情況下,java.io 包中的類總是根據目前使用者目錄來分析相對路徑名。此目錄由系統屬性 user.dir 指定,通常是 JAVA 虛擬機器的調用目錄。

首碼的概念用於處理 UNIX 平台的根目錄,以及 Microsoft Windows 平台上的盤符、根目錄和 UNC 路徑名,如下所示:
1、對於 UNIX 平台,絕對路徑名的首碼始終是 "/"。相對路徑名沒有首碼。表示根目錄的絕對路徑名的首碼為 "/" 並且沒有名稱序列。
2、對於 Microsoft Windows 平台,包含盤符的路徑名的首碼由磁碟機名和一個 ":" 組成:如果路徑名是絕對路徑名,後面可能跟著 "//"。UNC 路徑名的首碼是 "////";主機名稱和共用名稱是名稱序列中的前兩個名稱。沒有指定磁碟機的相對路徑名無首碼。

File 類的執行個體是不可變的;也就是說,一旦建立,File 對象表示的抽象路徑名將永不改變。

構造方法:
File(File parent, String child)
根據 parent 抽象路徑名和 child 路徑名字串建立一個新 File 執行個體。
File(String pathname)
通過將給定路徑名字串轉換成抽象路徑名來建立一個新 File 執行個體。
File(String parent, String child)
根據 parent 路徑名字串和 child 路徑名字串建立一個新 File 執行個體。
File(URI uri)
通過將給定的 file: URI 轉換成一個抽象路徑名來建立一個新的 File 執行個體。

方法請參考此API文檔

下面舉例說明:
(請預先建立檔案WriteFile.txt,程式運行後將會在同目錄下產生backup目錄,裡面為copy的檔案,包放在biz.1cn.stream裡面)

package biz.1cn.stream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;/** * @author chenrz(simon) * @date 2006-6-30 *       <p> *       JAVA檔案操作-更新檔案(www.1cn.biz) *       </p> */public class UpdateFile { public static void main(String[] args) throws IOException {  // 待覆制的檔案名稱  String fileName = "WriteFile.txt";  // 子目錄名  String childDir = "backup";  new UpdateFile().update(fileName, childDir); } public void update(String fileName, String childDir) throws IOException {  File f1, f2, child;  // 在fileName和childDir目前的目錄中分別建立檔案對象  f1 = new File(fileName);  child = new File(childDir);  if (f1.exists()) {   if (!child.exists())    child.mkdir();   // 在子目錄childDir中建立檔案對象   f2 = new File(child, fileName);   if (!f2.exists() || f2.exists()    && (f1.lastModified() > f2.lastModified()))    copy(f1, f2);   getInfo(f1);   getInfo(child);  } else   System.out.println(f1.getName() + " file not found!"); } /**  * 複製檔案f1至f2  *   * @param f1  *            源檔案  * @param f2  *            目標檔案  * @throws IOException  */ public void copy(File f1, File f2) throws IOException {  // 建立檔案輸入輸出資料流對象  FileInputStream is = new FileInputStream(f1);  FileOutputStream os = new FileOutputStream(f2);  // 設定讀取的位元組數  int count, n = 512;  byte buffer[] = new byte[n];  // 讀取輸入資料流  count = is.read(buffer, 0, n);  while (count != -1) {   os.write(buffer, 0, count);   count = is.read(buffer, 0, n);  }  // 關閉輸入輸出資料流  is.close();  os.close(); } /**  * 取得檔案或檔案夾資訊:路徑、修改時間  *   * @param file  * @throws IOException  */ public static void getInfo(File file) throws IOException {  // 初始化時間格式  SimpleDateFormat sdf =    new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  if (file.isFile())   // 返回抽象路徑名的絕對路徑、檔案長度、最後修改時間   System.out.println("<FILE>/t"    + file.getAbsolutePath() + "/t"   + sdf.format(new Date(file.lastModified())));  else {   System.out.println("/t" + file.getAbsolutePath());   File[] files = file.listFiles();   for (int i = 0; i < files.length; i++)    getInfo(files[i]);  } }}
package biz.1cn.stream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;/** * @author chenrz(simon) * @date 2006-6-30 *       <p> *       JAVA檔案操作-更新檔案(www.1cn.biz) *       </p> */public class UpdateFile { public static void main(String[] args) throws IOException {  // 待覆制的檔案名稱  String fileName = "WriteFile.txt";  // 子目錄名  String childDir = "backup";  new UpdateFile().update(fileName, childDir); } public void update(String fileName, String childDir) throws IOException {  File f1, f2, child;  // 在fileName和childDir目前的目錄中分別建立檔案對象  f1 = new File(fileName);  child = new File(childDir);  if (f1.exists()) {   if (!child.exists())    child.mkdir();   // 在子目錄childDir中建立檔案對象   f2 = new File(child, fileName);   if (!f2.exists() || f2.exists()    && (f1.lastModified() > f2.lastModified()))    copy(f1, f2);   getInfo(f1);   getInfo(child);  } else   System.out.println(f1.getName() + " file not found!"); } /**  * 複製檔案f1至f2  *   * @param f1  *            源檔案  * @param f2  *            目標檔案  * @throws IOException  */ public void copy(File f1, File f2) throws IOException {  // 建立檔案輸入輸出資料流對象  FileInputStream is = new FileInputStream(f1);  FileOutputStream os = new FileOutputStream(f2);  // 設定讀取的位元組數  int count, n = 512;  byte buffer[] = new byte[n];  // 讀取輸入資料流  count = is.read(buffer, 0, n);  while (count != -1) {   os.write(buffer, 0, count);   count = is.read(buffer, 0, n);  }  // 關閉輸入輸出資料流  is.close();  os.close(); } /**  * 取得檔案或檔案夾資訊:路徑、修改時間  *   * @param file  * @throws IOException  */ public static void getInfo(File file) throws IOException {  // 初始化時間格式  SimpleDateFormat sdf =    new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  if (file.isFile())   // 返回抽象路徑名的絕對路徑、檔案長度、最後修改時間   System.out.println("<FILE>/t"    + file.getAbsolutePath() + "/t"   + sdf.format(new Date(file.lastModified())));  else {   System.out.println("/t" + file.getAbsolutePath());   File[] files = file.listFiles();   for (int i = 0; i < files.length; i++)    getInfo(files[i]);  } }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.