File类的构造方法以及获取功能方法

File类的构造方法

File(File parent, String child)
从父抽象路径名和子路径名字符串创建新的 File实例。
File(String pathname)
通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
File(String parent, String child)
从父路径名字符串和子路径名字符串创建新的 File实例。
File(URI uri)
通过将给定的 file: URI转换为抽象路径名来创建新的 File实例。

 

复制代码
public class demg_02 {
    public static void main(String[] args) {
        File file = new File("E:\\s.txt");
        System.out.println(file);
        show01();
        show02("E:\\","s.txt");
    }
  //File(String parent, String child):
从父路径名字符串和子路径名字符串创建新的 File实例。好处:父路径和子路径,可以单独书写,使用起来非常灵活,父路径和子路径都可以变化
private static void show02(String f,String m) {
        File file = new File(f, m);
        System.out.println(file);
    }
  //File(String pathname):
通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
private static void show01() {
        File file = new File("E:\\s.txt");
        System.out.println(file);
    }
}
复制代码

 

File类获取功能的方法

String getAbsolutePath():返回此File的绝对路径名字字符串

String getPath():将此File转换为路径名字符串

String getName():返回由此File表示的文件或目录的名称

long length():返回由此File表示的文件的长度
复制代码
    public static void main(String[] args) {
        //String getAbsolutePath():返回此File的绝对路径名字字符串
        File file = new File("E:\\s.txt");
        String absolutePath = file.getAbsolutePath();
        System.out.println(absolutePath);
        //不管路径是目录还是文件返回的都是绝对路径
        File file1 = new File("s.txt");
        String absolutePath1 = file1.getAbsolutePath();
        System.out.println(absolutePath1);
        //String getPath():将此File转换为路径名字符串
        File file2 = new File("E:\\file\\bb.txt");
        String parent = file2.getParent();
        System.out.println(parent);
        //String getName():返回由此File表示的文件或目录的名称
        File file3 = new File("E:\\file\\bb.txt");
        String name = file3.getName();
        System.out.println(name);
        //long length():返回由此File表示的文件的长度
        File file4 = new File("E:\\file\\bb.txt");
        System.out.println(file4.length());
    }
复制代码

 

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