Home  >  Article  >  Java  >  Introduction to the construction method and common functions of the File class

Introduction to the construction method and common functions of the File class

王林
王林forward
2020-08-28 15:36:072681browse

Introduction to the construction method and common functions of the File class

首先我们来介绍一个路径的问题,路径分为绝对路径和相对路径。绝对路径是一个固定的路径,从盘符开始;相对路径相对于某个位置,在eclipse下是指当前项目下。

(推荐教程:java课程

(1)File中的构造方法:

File(String pathname):根据一个路径得到File对象

File(String parent, String child):根据一个目录和一个子文件/目录得到File对象

File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象

(2)File中的创建功能:

public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了

public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了

public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来

File file = new File("yyy.txt");		//可以加后缀,也可以不加后缀
    System.out.println(file.createNewFile());
File dir1 = new File("aaa");			//创建文件夹
        System.out.println(dir1.mkdirs());
File dir2 = new File("bbb.txt");    		//文件夹也可以加后缀
        System.out.println(dir2.mkdirs()); 
 
File dir3 = new File("ccc\\ddd");   
        System.out.println(dir3.mkdirs());  //创建多级目录(文件夹)

(3)重命名和删除功能:

public boolean renameTo(File dest):把文件重命名为指定的文件路径

public boolean delete():删除文件或者文件夹

注意:如果路径名相同,就是改名。如果路径名不同,就是改名并剪切。

        File file1 = new File("xxx.txt");
        File file2 = new File("ooo.txt");
        System.out.println(file1.renameTo(file2));
        File file3 = new File("D:\\XXX.txt");
        System.out.println(file2.renameTo(file3));
	File file1 = new File("yyy.txt");
        System.out.println(file1.delete());     //true
 
        File file2 = new File("aaa");
        System.out.println(file2.delete());     //true
 
        File file3 = new File("ccc");       	//如果删除一个文件夹,那么此文件夹必须为空
        System.out.println(file3.delete());     //false

(相关教程推荐:java入门教程

(4)判断功能:

public boolean isDirectory():判断是否是目录

public boolean isFile():判断是否是文件

public boolean exists():判断是否存在

public boolean canRead():判断是否可读

public boolean canWrite():判断是否可写

public boolean isHidden():判断是否隐藏

(5)获取功能:

public String getAbsolutePath():获取绝对路径public String getPath():获取路径public String getName():获取名称public long length():获取长度。字节数public long lastModified():获取最后一次的修改时间,毫秒值public String[] list():获取指定目录下的所有文件或者文件夹的名称数组public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组 

(6)文件名称过滤器的概述public String[] list(FilenameFilter filter)public File[] listFiles(FileFilter filter)

public static void main(String[] args) {
        File dir =  new File("D:\\");
        String[] arr = dir.list(new FilenameFilter(){
 
            @Override
            public boolean accept(File dir, String name) {
                //System.out.println(dir);      dir是盘符D盘
                //System.out.println(name);     name是文件名
                File file = new File(dir,name);
                return file.isFile() && file.getName().endsWith(".txt");
            }
        });
        //数组里存储的全是符合条件的
        for (String string : arr) {
            System.out.println(string); //BugReport.txt
        }
    }

The above is the detailed content of Introduction to the construction method and common functions of the File class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete