File类
File类用于封装一个路径,这个路径可以是从系统盘符开始的绝对路径,也可以是相对于当前目录的相对路径,File类内部封装的路径可以指向一个文件,也可以指向一个目录,在File类中提供了针对这些目录或文件的一些常规操作。
File类常用的构造方法
File(String pathname)//通过指定的一个字符串类型的文件路径来创建一个新的File对象
File(String parent,String child)//根据指定的一个字符串类型的父路径和一个字符串类型的子路径创建一个File对象
File(File parent,String child)//根据指定的File类的父路径和字符串类型的子路径创建一个File对象
查看文件的相应信息
package JS; import java.io.File; public class XX { public static void main(String[] args) { File file=new File("example.txt"); //获取文件名称 System.out.println("文件名称:"+file.getName()); //获取文件的相对路径 System.out.println("文件的相对路径:"+file.getPath()); //获取文件的绝对路径 System.out.println("文件的绝对路径:"+file.getAbsolutePath()); //获取文件的父路径 System.out.println("文件的父路径:"+file.getParent()); //判断文件是否可读 System.out.println(file.canRead() ?"文件可读":"文件不可读"); //判断文件是否可写 System.out.println(file.canWrite() ?"文件可写":"文件不可写"); //判断是否是同一个文件 System.out.println(file.isFile() ?"是一个文件":"不是一个文件"); //判断是否是同一个目录 System.out.println(file.isDirectory() ?"文件是一个目录":"文件不是一个目录"); //得到文件最后的修改时间 System.out.println("最后修改时间为:"+file.lastModified()); //得到文件的大小 System.out.println("文件的大小为:"+file.length()+"bytes"); //是否成功删除文件 System.out.println("是否成功删除文件"+file.delete()); } }
遍历目录下的文件
通过list()方法可以遍历某个指定目录下的所有文件名称
package JhiShi; import java.io.File; public class Example01 { public static void main(String[] args) throws Exception{ File file=new File("C:\\Users\\lenovo\\IdeaProjects\\java se"); if(file.isDirectory()){ String[] names=file.list(); for (String name:names){ System.out.println(name); } } } }
先通过File类里面的isDirectory()方法判断路径指向的是否为存在的目录,存在就调用list()方法,并且获得String类型的数组names,数组中包含这个目录下的所有文件的文件名,然后循环遍历数组的names,依次打印出每个文件的名字。
文件过滤器
package JhiShi; import java.io.File; import java.io.FilenameFilter; public class Example02 { public static void main(String[] args) throws Exception{ File file=new File("C:\\Users\\lenovo\\IdeaProjects\\java se"); FilenameFilter filter=new FilenameFilter() { @Override public boolean accept(File dir, String name) { File currFile=new File(dir,name); if(currFile.isFile()&&name.endsWith(".txt")){ return true; }else{ return false; } } }; if(file.exists()){ String[] lists=file.list(filter); for (String name:lists){ System.out.println(name); } } }
对子目录进行遍历
package JhiShi; import java.io.File; public class Example03 { public static void main(String[] args) throws Exception{ File file=new File("C:\\Users\\lenovo\\IdeaProjects\\java se"); fileDir(file); } public static void fileDir(String[] args) { File[]files=dir.listFiles(); for (File file:files){ if(file.isDirectory()){ fileDir(file); } System.out.println(file.getAbsoluteFile()); } } }
通过一个静态方法fileDir(),用于接收一个表示目录的File对象,先调用listFile()方法把该目录下所有的子目录和文件存到一个File类型的数组files中,然后遍历数组files,并且对遍历对象进行判断,如果是目录就从新调用fileDir()方法进行递归,如果是文件则输出文件的路径。
删除文件及目录
package JhiShi; import java.io.File; public class Example03 { public static void main(String[] args) { File file=new File("C:\\ABC"); deleteDir(file); } public static void deleteDir(String[] args) { if(dir.exists){ File[]files=dir.listFiles(); for(File file:files){ if(files.isDirectory()){ deleteDir(file); }else{ file.delete(); } } dir.delete(); } } }
定义了一个删除目录的静态方法deleteDir()来接收一个File类型的参数,调用listFiles()方法把这个目录下所有的子目录和文件保存到一个File类型的数组files中,然后遍历files,如果是目录从新调用deleteDir()方法进行递归,如果是文件则直接调用File的delete()方法删除,当删除完这个目录下的所有文件时,再删除这个目录。
注意:Java删除目录是从虚拟机直接删除而不是回收站,一旦删除无法恢复
The above is the detailed content of Java File class common methods and file filter example analysis. For more information, please follow other related articles on the PHP Chinese website!

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

About SpringCloudAlibaba microservices modular development using SpringCloud...

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)