1.初始File----三种创建File实例方法

public class Test01 {
    public static void main(String[] args) {
        method1();

        method2();

        method3();

    }

    private static void method3() {
        // File (File parent,String child) 从父抽象路径名和子路径名字符串创建新的File实例
        File file1 = new File("F:\\test");
        String path="a.txt";
        File file = new File(file1, path);
    }

    private static void method2() {
        // File (String parent,String child) 从父路径名字字符串和子路径名字字符串创建新的File实例
        String path1 = "F:\\test";
        String path2 = "a.txt";
        File file = new File(path1, path2);//把两个路径拼接
    }

    private static void method1() {
        //File(String pathname)  通过将给定的路径名 字符串转换为抽象路径名 来创建新的File实例
        String path = "F:\\test\\a.txt";
        File file = new File(path);
//        q:为什么要把字符串表示形式的路径 变成File对象?
//        a:就是为了使用File类里面的方法.
    }
}
posted @ 2024-03-28 14:36  冷风5997  阅读(60)  评论(0)    收藏  举报