345绝对路径和相对路径和346File的构造方法

概念:

路径:

绝对路径:

是一个完整的路径以盘符(c:,D:)开始的路径c:a. txtC: \Users\itcast\\Ideaprojects l\shangyuan \\123.txtD:l\demo\\b. txt

相对路径:

是一个简化的路径相对指的是相对于当前项目的根目录(C: Userslitcast\\Ideaprojects\\shangyuan)

如果使用当前项目的根目录,路径可以简化书写c: Users litcost \\Ideaprojectsllshungyuan1123.txt-)

简化为:123.txt(可以省略项目的根目录注意:

1.路径是不区分大小写

2.路径中的文件名称分隔符 windows使用反斜杠,反斜杠是转义字符,两个反斜杠代表一个普通的反斜杠

File构造方法

 

1.File(String pathname):
我的电脑中并没有itcast这个文件,但是他没有报错,所以他只是抽象路径的表示形式,不用一定存在。
他输出了File类的内容,所以他重写了toString方法。
2.File(String parent,String child):
我的电脑中并没有itcast这个文件,但是他没有报错,所以他只是抽象路径的表示形式,不用一定存在。
他输出了File类的内容,所以他重写了toString方法。
3.File(File parent,String child):
我的电脑中并没有itcast这个文件,但是他没有报错,所以他只是抽象路径的表示形式,不用一定存在。
他输出了File类的内容,所以他重写了toString方法。

 public static void main(String[] args) {
        show01();
        show02("D","a.txt");
        show03();
    }
    private static void show02(String parent ,String chid) {
        File file = new File(parent, chid);
        System.out.println(file);
    }
    
    private static void show03() {
        File f1 = new File("D:\\");
        File f2 = new File("hello.java");
        System.out.println(f2);
    }
    private static void show01() {
        File a1 = new File("D:\\file\\a.txt");
        System.out.println(a1);

        File a2 = new File("\"D:\\\\file");
        System.out.println(a2);

        File a3 = new File("a.txt");
        System.out.println(a3);
         
    }
}

 

posted @ 2022-07-09 09:02  ja不会va  阅读(39)  评论(0)    收藏  举报