File类详解

1.java.io.File类是java中很常见的一个类。该类有一下特性:

    (1)封装了文件或路径。

    (2)不能用来创建文件,即在程序中创建一个File对象,并不会在机器上创建一个文件(感兴趣的同学可以去实践一下)。那创建的一个File对象不会创建一个文件,那这个File对象到底是什么呢?我个人理解这个File对象其实就是机器上某个文件的对象的引用。

    (3)不能用来向文件中写内容。

    (4)不能用来从文件中度内容。

2.这是我在本地建的文件夹和文件。用于下文的测试:

    目录结构为:ParentDir文件夹下面有三个文件夹:ChildDir1,ChildDir2,ChildDir3,以及两个文本文件:ChildFile1.txt,ChildFile2.txt。在ChildDir1目录下还有两个文本文件:file1.txt,file2.txt。

    

    

3.构造方法:

    (1)为特定路径名创建一个File对象,这里的路径名可以是一个目录也可以是一个文件

public File(String pathname) {
        if (pathname == null) {
            throw new NullPointerException();
        }
        this.path = fs.normalize(pathname);
        this.prefixLength = fs.prefixLength(this.path);
    }

    举例:

public class FileTest {
    public static void main(String[] args) {
        File newFile1 = new File("F:\\ParentDir\\ChildDir1");
        System.out.println(newFile1.exists());
        File newFile2 = new File("F:\\ParentDir\\ChildFile1.txt");
        System.out.println(newFile2.exists());
    }
}

    (2)为目录parent下的child创建一个File对象,这个child可以是一个文件名也可以是一个目录

public File(String parent, String child) {
        if (child == null) {
            throw new NullPointerException();
        }
        if (parent != null) {
            if (parent.equals("")) {
                this.path = fs.resolve(fs.getDefaultParent(),
                                       fs.normalize(child));
            } else {
                this.path = fs.resolve(fs.normalize(parent),
                                       fs.normalize(child));
            }
        } else {
            this.path = fs.normalize(child);
        }
        this.prefixLength = fs.prefixLength(this.path);
    }

   举例:

public class FileTest {
    public static void main(String[] args) {
        File newFile1 = new File("F:\\ParentDir","ChildDir1");
        System.out.println(newFile1.exists());
        File newFile2 = new File("F:\\ParentDir","ChildFile1.txt");
        System.out.println(newFile2.exists());
    }
}

     (3)为目录parent下的child创建一个File对象,这个parent是一个File对象

public File(File parent, String child) {
        if (child == null) {
            throw new NullPointerException();
        }
        if (parent != null) {
            if (parent.path.equals("")) {
                this.path = fs.resolve(fs.getDefaultParent(),
                                       fs.normalize(child));
            } else {
                this.path = fs.resolve(parent.path,
                                       fs.normalize(child));
            }
        } else {
            this.path = fs.normalize(child);
        }
        this.prefixLength = fs.prefixLength(this.path);
    }

举例:

public class FileTest {
    public static void main(String[] args) {
        File newFile1 = new File("F:\\ParentDir");
        System.out.println(newFile1.exists());
        File newFile2 = new File(newFile1,"ChildFile1.txt");
        System.out.println(newFile2.exists());
    }
}

4.常见方法:

    (1)exists():File对象表示的文件名或路径是否存在,存在返回true。

    (2)canRead():File对象表示的文件名或路径存在且可读,存在返回true。

    (3)canWrite():File对象表示的文件名或路径存在且可写,存在返回true。

    (4)isFile():File对象是一个文件,返回true。

    (5)isDirectory():File对象表示的是一个目录,返回true。

    (6)isAbsolute():File对象表示的文件是使用绝对路径名创建的,返回true。

    (7)isHidden():File对象表示的文件属性是隐藏的,返回true。

    (8)getAbsolutePath():返回File对象表示的文件或目录的绝对路径。

    (9)getCanonicalPath():返回File对象表示的文件或目录的绝对路径,用于unix系统。

    (10)getName():返回File对象表示的文件或目录的名称。

    (11)getPath():返回File对象表示的文件或目录的完整路径。

    (12)getParent():返回File对象表示的文件或目录的父目录的完整路径。

    (13)lastModified():返回File对象表示的文件或目录的最后一次修改时间。

    (14)length():返回File对象表示的文件或目录的长度。

    (15)listFiles():如果返回File对象表示的是一个目录,则返回该目录下所有的文件和目录。

    (16)delete():删除File对象表示的文件或目录。

    (17)renameTo(File file):重命名。

猜你喜欢

转载自blog.csdn.net/Dream_Ryoma/article/details/80859097
今日推荐