zoukankan      html  css  js  c++  java
  • File类

    File类

    File类位于Java.io包中,是文件和文件目录(文件夹)的抽象表示形式,与平台无关。

    File的作用

    • 创建和删除文件或者文件目录;
    • 重命名文件或者文件目录;
    • 判断文件的读写权限及是否存在;
    • 设置和查询文件的最近修改时间;
    • 构造文件流时可以将File类的对象作为参数传递给流的构造器;

    想要在Java程序中表示一个真实存在的文件或者目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或者目录。

    当硬盘中真有一个真实的文件或者文件夹存在时,创建File对象时,各个属性会显式赋值(比如文件大小对应length属性)。当硬盘中没有真实文件或者目录对应时,那么创建File对象时,除了指定的目录和路径外,其他的属性都是取成员变量的默认值。

    File类的常用构造器

    // 以pathname 为路径创建File对象,可以是绝对路径或者相对路径
    public File(String pathname) {
        if (pathname == null) {
            throw new NullPointerException();
        }
        this.path = fs.normalize(pathname);
        this.prefixLength = fs.prefixLength(this.path);
    }
    
    // 以parent为父路径,child为子路径创建File对象
    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);
    }
    
    // 根据一个父File对象和子文件路径创建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);
    }
    

    路径中的每级目录之间用一个路径分隔符隔开,路径分隔符和系统有关:

    • windows和dos系统默认使用""来表示
    • unix和url使用"/"来表示

    Java程序支持跨平台运行,因此路径分隔符要慎用。为了解决这个隐患,File类提供了一个常量:

     public static final String separator = "" + separatorChar;
    

    该常量根据操作系统,动态的提供分隔符。

    public static void main(String[] args) {
    
        File file = new File("F:\IdeaWorkplace\datas\name.txt");
        File file2 = new File("F:"+File.separator+"IdeaWorkplace"+File.separator+"datas"+File.separator+"name.txt");
    
        System.out.println("F:"+File.separator+"IdeaWorkplace"+File.separator+"datas"+File.separator+"name.txt");
        System.out.println("F:\IdeaWorkplace\datas\name.txt");
    
    }
    

    输出结果:

    F:IdeaWorkplacedatas
    ame.txt
    F:IdeaWorkplacedatas
    ame.txt
    

    File类的常用方法

    1、获取功能

    public static void main(String[] args) {
    
        File file = new File("F:"+File.separator+"IdeaWorkplace"+File.separator+"datas"+File.separator+"name.txt");
    
        System.out.println("file的完整路径为:" + file.getAbsoluteFile());
        System.out.println("file的路径:" + file.getPath());
    
        System.out.println("file的名称:"+file.getName());
        System.out.println("file的父目录路径:" + file.getParent());
    
        System.out.println("file的长度:" + file.length());
    
        Date date = new Date(file.lastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("file的最后一个修改时间:"+sdf.format(date));
    
    
        // 如下两个方法适用于文件目录
        File file1 = new File("F:" + File.separator + "IdeaWorkplace" + File.separator + "datas");
    	
        // 获取指定目录下的所有文件或者文件目录的名称数组
        String[] list = file1.list();
        System.out.println(Arrays.toString(list));
    	
        // 获取指定目录下的所有文件或者文件目录的File数组
        File[] files = file1.listFiles();
        System.out.println(files.length);
        
    }
    

    输出结果:

    file的完整路径为:F:IdeaWorkplacedatas
    ame.txt
    file的路径:F:IdeaWorkplacedatas
    ame.txt
    file的名称:name.txt
    file的父目录路径:F:IdeaWorkplacedatas
    file的长度:23
    file的最后一个修改时间:2021-05-21 10:44:46
    [name.txt, name2.txt]
    2
    

    2、重命名功能

    public boolean renameTo(File dest)
    

    使用该方法进行重命名,必须保证调用该方法的file对象有真实存在的文件在磁盘中,dest文件对象不能有真实存在的文件在磁盘中。

    public static void main(String[] args) {
    
            File file1 = new File("./datas/name3.txt");
            File file2 = new File("./datas/name.txt");
            
            boolean b = file1.renameTo(file2);
            System.out.println(b);
    }
    

    执行之前,file1对应的文件所在目录:

    image-20210521141917901

    执行完毕之后,输出结果为true,file1所在目录:

    image-20210521141944852

    3、判断功能

    public boolean isDirectory() 判断是否是文件夹
    public boolean isFile() 判断是否是文件
    public boolean exists() 判断是否存在
    public boolean canRead() 判断是否可读
    public boolean canWrite() 判断是否可写
    public boolean isHidden()  是否是隐藏文件或者文件目录
    

    4、创建功能

    public boolean createNewFile() 创建文件,如果文件已经存在,则不创建,返回false。
    public boolean mkdir() 创建文件目录,如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
    public boolean mkdirs() 创建文件目录,如果上层文件目录不存在,一并创建。
    

    注意事项:如果你创建文件或者文件目录没有写盘符路径那么默认在项目路径下 。

    5、删除功能

    public boolean delete() 删除文件或者文件夹
    

    删除注意事项:Java中的删除不走回收站,要删除一个文件目录,请注意该文件目录内不能包含文件或者子目录。

  • 相关阅读:
    发工资
    洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
    洛谷P2169 正则表达式
    洛谷[LnOI2019]长脖子鹿省选模拟赛t1 -> 快速多项式变换
    洛谷 P1690 贪婪的Copy
    洛谷P1090 合并果子
    洛谷P1886 滑动窗口
    洛谷CF784E Twisted Circuit
    洛谷P2430 严酷的训练
    开博客第一天祭!!!
  • 原文地址:https://www.cnblogs.com/yxym2016/p/14794920.html
Copyright © 2011-2022 走看看