Home > Java > Javagetting Started > body text

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

王林
Release: 2020-08-28 15:36:07
forward
2820 people have browsed it

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());
Copy after login
File dir1 = new File("aaa");			//创建文件夹
        System.out.println(dir1.mkdirs());
Copy after login
File dir2 = new File("bbb.txt");    		//文件夹也可以加后缀
        System.out.println(dir2.mkdirs()); 
 
File dir3 = new File("ccc\\ddd");   
        System.out.println(dir3.mkdirs());  //创建多级目录(文件夹)
Copy after login

(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));
Copy after login
	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
Copy after login

(相关教程推荐: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
        }
    }
Copy after login

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!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template