Java中的文件对象
File对象代表磁盘上的实际文件/目录。以下是在Java中创建文件对象的构造函数的列表-
假设在给定位置存在对象,则命令行的第一个参数将被视为路径,并将执行以下代码-
示例
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("文件名是:"+my_file.getName());
System.out.println("The path to the file is: "+my_file.getPath());
System.out.println("该文件的绝对路径为:" +my_file.getAbsolutePath());
System.out.println("父目录是:"+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的类包含主要功能,并定义了一个字符串,该字符串包含在命令行中传递的第一个参数。文件的详细信息会显示在屏幕上,其中包括文件名,文件路径,文件的绝对路径以及文件的父目录。
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志