File类获取功能的方法和File类获取判断功能的方法

  

获取功能的方法
public string getAbsolutePath()`︰返回此File的绝对路径名字符串。public string getPath()︰将此ile转换为路径名字符串。
public string getName():返回由此File表示的文件或目录的名称。public long length()︰返回由此File表示的文件的长度。

复制代码
public static void main(String[] args) {
        showe1();
        show02();
        show3();
        showe4();
    }

    /*
        public string getAbsolutePath():返回此File的绝对路径名字符串。
        获取的构造方法中传速的路径
        无论路径是绝对的还是相对的, getAbsolutePath方法返回的都是绝对路径
     */

    private static void showe1() {
        File f1 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt");
        String absolutePath1 = f1.getAbsolutePath();
        System.out.println(absolutePath1);
        File f2 = new File("a.txt");
        String absolutePath2 = f2.getAbsolutePath();
        System.out.println(absolutePath2);
    }

    /*
        public string getPath() :将此FiLe转换为路径名字符串。
        获取的构造方法中传递的路径

         toString方法调用的就是getPath方法
         源码:
            public string toString( ) {
            return getPath( );
     */
    private static void show02() {
        File f1 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt");
        File f2 = new File("a.txt");
        String path1 = f1.getPath();
        System.out.println(path1);
        String path2 = f2.getPath();
        System.out.println(path2);
        System.out.println(f1);
        System.out.println(f1.toString());
    }

    /*
        public string getName()︰返回由此File表示的文件或目录的名称。
        获取的就是构造方法传递路径的结尾部分(文件/文件夹)

     */
    private static void show3() {
        File f1 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt");
        String name1 = f1.getName();
        System.out.println(name1);//a.txt
        File f2 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077");
        String name2 = f2.getName();
        System.out.println(name2);
    }
    /*
            public long length():返回由此File表示的文件的长度。获取的是构造方法指定的文件的大小,以字节为单位
            注意:
            文件夹是没有太小概念的,不能获取文件夹的大小
            如果构造方法中给出的路径不存在,那么Length方法返回6
     */
    private static void showe4() {
        File f1 = new File("D:\\桌面\\tupian\\wallhaven-dpj6ko_1920x1080.png");
        long l1 = f1.length();
        System.out.println(l1);
        File f2 = new File("D:\\桌面\\tupian\\wallhaven-dpj6ko_1920x100.png");//没有这个图片,返回的值0
        System.out.println(f2.length());
        File f3 = new File("D:\\桌面\\tupian");
        System.out.println(f3.length());//文件夹没有大小概念的
    }
复制代码

 

File类获取判断功能的方法

复制代码
 public static void main(String[] args) {


    }
    /*
            public boolean exists() :此File表示的文件或目录是否实际存在。
            public boolean isDirectory() :此File表示的是否为目录。
            public boolean isFile():此File表示的是否为哀件。
         */
    private static void show1(){
        File f1 = new File( "D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt");
        System.out.println(f1.exists());
        File f2 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a");
        System.out.println(f2.exists());
        File f3 = new File( "shungyuan.iml" );
        File f4 = new File(  "a.txt");
        System.out.println(f4.exists());

    }
    /*
        public boolean isDirectory() : 此FiLe表示的是否为目录。
            用于判断构造方法中给定的路径是否以文件夹结尾
            是:true
            否:false
        public boolean isFile() :此FiLe表示的是否为文件。
            用于判断构造方法中给定的路径是否以文件结尾
                 是:true
                否:false
    注意:
        电脑的硬盘中只有文件/文件夹,两个方法是互斥
        这两个方法使用前提,路径必须是存在的,否则都返回false

     */
    private static void show2() {
        File f1 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt");
        //不存在,就没有必要获取
        if (f1.exists()) {
            System.out.println(f1.isDirectory());
            System.out.println(f1.isFile());
        }
        File f2 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt");
        if (f2.exists()) {
            System.out.println(f2.isDirectory());
        }
        File f3 = new File("D:\\taitanyunluo\\END\\Cyberpunk 2077\\a.txt\\shungyuan.iml");
        if (f3.exists()) {
            System.out.println(f3.isDirectory());
            System.out.println(f3.isFile());
        }
    }
复制代码

 

posted @   漁夫  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示