File类的构造方法

package com.learn.demo01.File;

import java.io.File;

public class Demo02File {
    public static void main(String[] args) {
        /*
            File类的构造方法
         */
//        show02("D:\\","a.txt");//D:\a.txt
//        show02("d:\\","a.txt");//d:\a.txt
        show03();
//
//        File f = new File("D:\\Users\\learn\\IdeaProjects\\leon");
//        long length = f.length();
//        System.out.println(length);
    }

    /*
        File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
        参数:把路径分成了两部分
            File parent:父路径
            String child:子路径
        好处:
             父路径和子路径,可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
             父路径是File类型,可以使用File的方法对路径进行一些操作,再使用路径创建对象
     */
    private static void show03() {
        File parent = new File("D:\\");
        File file = new File(parent,"hello.java");
        System.out.println(file);//D:\hello.java
    }

    /*
        File(String parent, String child) 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
        参数:把路径分成了两部分
            String parent:父路径
            String child:子路径
        好处:
            父路径和子路径,可以单独书写,使用起来非常灵活;父路径和子路径都可以变化
     */
    private static void show02(String parent, String child) {
        File file = new File(parent,child);
        System.out.println(file);//D:\a.txt
    }

    /*
        File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
        参数:
            String pathname:字符串的路径名称
            路径可以是以文件结尾,也可以是以文件夹结尾
            路径可以是相对路径,也可以是绝对路径
            路径可以是存在,也可以是不存在
            创建File对象,只是把字符串路径封装为File对象,不考虑路径的真假情况
     */
    private static void show01() {
        File f1 = new File("D:\\idea\\IdeaProjects\\08_FileAndRecursion\\src\\com\\learn\\demo01\\a.txt");
        System.out.println(f1);//重写了Object类的toString方法 D:\idea\IdeaProjects\08_FileAndRecursion\src\com\learn\demo01\a.txt

        File f2 = new File("D:\\idea\\IdeaProjects\\08_FileAndRecursion\\src\\com\\learn\\demo01");
        System.out.println(f2);//D:\idea\IdeaProjects\08_FileAndRecursion\src\com\learn\demo01

        File f3 = new File("b.txt");
        System.out.println(f3);//b.txt
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值