//Demonstrate File.
import java.io.File;
class FileDemo{
static void p(String s){
System.out.println(s);
}
public static void main(String[] args)
{
File f1 = new File("c:/boot.ini");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("ABS Path: " + f1.getAbsolutePath());
p(f1.exists() ? "exists":"does not exist");
p(f1.canWrite() ? "is writeable":"is not writeable");
p(f1.canRead() ? "is readable":"is not readable");
p("is " + (f1.isDirectory() ? "":"not" + "a directory"));
p(f1.isFile() ? "is normal file":"might be a named pipe");
p(f1.isAbsolute() ? "is absolute":"is not absolute");
p("File last modified: " + f1.lastModified());
p("File Size: " + f1.length() + " Bytes");
p("File Hidden: " + f1.isHidden() );
}
}
本文提供了一个使用 Java 的 File API 来操作文件的具体示例。该示例展示了如何获取文件名、路径、绝对路径等信息,并检查文件是否存在、是否可读写、是否为目录等属性。
3万+

被折叠的 条评论
为什么被折叠?



