JavaSE 学习参考:File文件类(2)

来源:互联网 发布:js是什么语言 编辑:程序博客网 时间:2024/05/16 01:07

   

java.io.File提供了一组用于读取文件的属性方法,如判定是否可执行,可读,可写及最近修改时间:

² boolean canExcutable()判定是否是可执行文件。

² boolean canWrite()判定是否是可写。

² boolean canRead()判定是否是可读。

² long lastModified ()获得文件最后修改时间。

 

示例代码:

   public class Test {

public static void main(String[] args) {

File file = new File("src\\weizhang\\Test.java");

if (file.isFile()) {

String str1=file.canExecute()?"可执行文件":"不可执行文件";

System.out.println(str1);

String str2=file.canRead()?"可读文件":"不可读文件";

System.out.println(str2);

String str3=file.canWrite()?"可写文件":"不可写文件";

System.out.println(str3);

Date date=new Date(file.lastModified());

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

String str4=sdf.format(date);

System.out.println(str4);

}

}

}

 

运行程序,结果如下:

可执行文件

可读文件

可写文件

2017-04-01

 

 

 

 

0 0