File类获取功能的方法

/*
getAbsolutePath()
        返回此抽象路径名的绝对路径名字符串。
getPath()
        将此抽象路径名转换为一个路径名字符串。
getName()
        返回由此抽象路径名表示的文件或目录的名称。
length()
        返回由此抽象路径名表示的文件的长度。

        */

import java.io.File;


public class Demo03File {
    public static void main(String[] args) {
        show01();
        show02();
        show03();
        show04();


    }

    private static void show04() {//文件夹是没有大小概念的
        File f1=new File("C:\\Users\\cy\\Desktop\\1.jpg");
        File f2=new File("C:\\Users\\cy\\Desktop\\2.jpg");
        File f3=new File("C:\\Users\\cy\\Desktop\\miniprogram-2");
        long length = f1.length();
        long length1 = f2.length();
        long length2 = f3.length();
        System.out.println(length);
        System.out.println(length1);
        System.out.println(length2);

    }

    private static void show03() {//结尾部分文件或文件夹名
        File f1=new File("C:\\Users\\cy\\Desktop\\list.txt");
        String name1=f1.getName();
        System.out.println(name1);//list.txt
        File f2=new File("C:\\Users\\cy\\Desktop");
        String name2 = f2.getName();
        System.out.println(name2);//Desktop

    }

    private static void show02() {
        File f1=new File("C:\\Users\\cy\\Desktop\\list.txt");
        File f2=new File("list.txt");
        String path1 = f1.getPath();
        System.out.println(path1);//C:\Users\cy\Desktop\list.txt

        String path2 = f2.getPath();
        System.out.println(path2);//list.txt

        System.out.println(f1.toString());//调用getpath方法  C:\Users\cy\Desktop\list.txt

    }

    private static void show01() {
        File f1=new File("C:\\Users\\cy\\Desktop\\list.txt");
        String absolutePath = f1.getAbsolutePath();
        System.out.println(absolutePath);


        File f2=new File("list.txt");
        String absolutePath1 = f2.getAbsolutePath();
        System.out.println(absolutePath1);

    }
}

 

posted @ 2020-10-04 14:11  159566  阅读(137)  评论(0编辑  收藏  举报