Java 中的檔案物件
File 物件表示磁碟上的實際檔案/目錄。以下列出了一些建構函式,可在 Java 中建立 File 物件 −
| 序號 | 方法和說明 |
|---|---|
| 1 | File(File parent, String child)此建構函式從父抽象路徑名和子路徑名字串中建立一個新的 File 例項。 |
| 2 | File(String pathname)此建構函式透過將給定的路徑名字串轉換為抽象路徑名,建立一個新的 File 例項。 |
| 3 | File(String parent, String child)此建構函式從父路徑名字串和子路徑名字串中建立一個新的 File 例項。 |
| 4 | File(URI uri)此建構函式透過將給定的 file: URI 轉換為抽象路徑名,建立一個新的 File 例項。 |
如果給定位置中存在一個物件,則命令列中的第一個引數將被視為路徑,並且將執行以下程式碼 −
示例
import java.io.File;
public class Demo{
public static void main(String[] args){
String file_name =args[0];
File my_file = new File(file_name);
System.out.println("File name is :"+my_file.getName());
System.out.println("The path to the file is: "+my_file.getPath());
System.out.println("The absolute path to the file is:" +my_file.getAbsolutePath());
System.out.println("The parent directory is :"+my_file.getParent());
if(my_file.exists()){
System.out.println("Is the file readable"+my_file.canRead());
System.out.println("The size of the file in bytes is "+my_file.length());
}
}
}輸出
The details about the file will be displayed here.
Demo 類的 main 函式中有一個字串,該字串儲存命令列中傳入的第一個引數。該檔案的詳細資訊將列印在螢幕上,其中包括檔名、檔案路徑、檔案的絕對路徑以及該檔案的父目錄。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP