filedemo.java~12~
来自「java2参考大全上的例子的源码和自己的理解.」· JAVA~12~ 代码 · 共 47 行
JAVA~12~
47 行
package file方法;
/**
File Name: COPYRIGHT.txt
Path: java\COPYRIGHT.txt
Abs Path: D:\JAVA Programe\File方法\java\COPYRIGHT.txt
Parent: java
exists
is writeable
is readable
is not a directory
is normal file
is not absolute
File last modified: 1110179708000
File size: 54 Bytes
大多数File方法是自说明的,但isFile( )和isAbsolute( )不是。isFile( )在被文件调用时返
回true,在被目录调用时返回false。并且,isFile( )被一些专用文件调用时返回false,例如设
备驱动程序和命名管道,所以该方法可用来判定文件是否作为文件执行。isAbsolute( )方法
在文件拥有绝对路径时返回true,若是相对路径则返回false。
*/
// 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("D:/JAVA Programe/File方法/java/COPYRIGHT.txt");
File f1 = new File("java/COPYRIGHT.txt");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Abs Path: " + f1.getAbsolutePath());
p("Parent: " + f1.getParent());
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");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?