Java_基础—File类的概述和构造方法

package com.soar.file;

import java.io.File;

public class Demo1_File {
    /*
* A:File类的概述
    * File更应该叫做一个路径
        * 文件路径或者文件夹路径  
        * 路径分为绝对路径和相对路径
        * 绝对路径是一个固定的路径,从盘符开始
        * 相对路径相对于某个位置,在eclipse下是指当前项目下,在dos下
    * 查看API指的是当前路径
    * 文件和目录路径名的抽象表示形式
* B:构造方法
    * File(String pathname):根据一个路径得到File对象
    * File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
    * File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
* C:案例演示
    * File类的构造方法
     */
    public static void main(String[] args) {
        //file();
        //file2();
        //将父级路径封装成一个File对象,可以更方便的使用File里的功能
        File parent = new File("D:\\A_Video\\IT_course");
        String child  = "JavaWeb";
        File file = new File(parent,child);
        System.out.println(file.exists());  //true
        System.out.println(parent.exists()); //true
    }

    private static void file2() {
        String parent = "D:\\A_Video\\IT_course";
        String child = "JavaWeb";
        File file = new File(parent,child);
        System.out.println(file.exists());  //true
    }

    private static void file() {
        File file = new File("D:\\A_Video\\IT_course\\JavaWeb");    //绝对路径
        System.out.println(file.exists());  //判断当前路径是否存在 true
        //在该项目中创建一个xxx.txt
        File file2 = new File("xxx.txt");       //相对路径  
        System.out.println(file2.exists());     //true
        File file3 = new File("yyy.txt");       //没有该文件
        System.out.println(file3.exists());     //false
    }

}

猜你喜欢

转载自blog.csdn.net/qq_36838191/article/details/84334963