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