当前位置:网站首页>Use of file class filenamefilter & filefilter in io

Use of file class filenamefilter & filefilter in io

2022-06-25 00:42:00 Aiqing

File Class Basics :
1. structure File Class object (3 Ways of planting )
 Insert picture description here

		File a=new File("E:\\java\\day22\\day22");
		System.out.println(a);
		File b=new File("E:\\java\\day22","day22");
		System.out.println(b);
		File c=new File("E:\\java\\day22");
		File d=new File(c,"day22");
		System.out.println(d);
 result :	E:\java\day22\day22
		E:\java\day22\day22
		E:\java\day22\day22

2.listFiles() Methods to introduce : Get all files in a directory or folders in a directory
 Insert picture description here

		File a=new File("E:\\java\\day22\\day22");
		String [] list=a.list();// Get the name of the current file and folder in the directory .
		for(String k:list) {
    
			System.out.println(k);
		}
		System.out.println();
		
		File[] list2=a.listFiles();// Get the current file and file object in the directory , Just get the file object 
		for(File k:list2) {
    
			System.out.println(k.getName());//.getname() See common methods at the end of the article 
			// If not getName The result will be prefixed with the pathname E:\java\day22\day22\
		}
		System.out.println();
 The two results are the same :	250.txt
				day22-(File) Lesson plans .doc
				day22-File.pptx
				day22_code
				day22_source
				day22_video
				day22 Homework & preview 
				IO The first part .xmind

FilenameFilter Use : It is mainly used to filter the files needed

public class test2 {
    
	public static void main(String[] args) {
    
		File a=new File("E:\\java\\day22\\day22");
		File[] b=a.listFiles(new myfilenamefilter());// Get the file with the specified extension , Due to the extension filtering for all files , So calling methods requires passing filters 
		for(File c:b) {
    
			System.out.println(c);
		}
	}
}
// Custom class inheritance  FilenameFilter Interface , You can write filter conditions for any file you need 
class myfilenamefilter implements FilenameFilter{
    
	public boolean accept(File dir, String name) {
    
		return name.endsWith(".txt");
	}
}
 result :250.txt

FileFilter Use : It is mainly used to filter the directories needed for file directory filtering

public class test3 {
    
	public static void main(String[] args) {
    
		File a=new File("E:\\java\\day22\\day22");
		File[] b=a.listFiles(new myfilefilter());
		for(File k:b) {
    
			System.out.println(k.getName());
		}
	}
}
class myfilefilter implements FileFilter{
    
	public boolean accept(File path) {
    
		return path.isDirectory();
	}
}
 result :	day22_code
		day22_source
		day22_video
		day22 Homework & preview 

File Common methods
One 、 Get file information class
 Insert picture description here
Code demonstration :

		File a=new File("E:\\java\\day22\\day22");
		String abspath=a.getAbsolutePath();
		String path=a.getPath();
		String filename=a.getName();
		long size=a.length();
		System.out.println("abspath:"+abspath);
		System.out.println("path:"+path);
		System.out.println("fname:"+filename);
		System.out.println("size:"+size);
 result :
		abspath:E:\java\day22\day22
		path:E:\java\day22\day22
		fname:day22
		size:4096

Two 、 Creation and deletion of files and folders
 Insert picture description here

		File a=new File("E:\\java\\day22\\day22");
		File x=new File("E:\\java\\day22\\day22\\nice");
		File java=new File(a,"250.txt");
		boolean b1=java.createNewFile();// If the file is not created, return ture, If the file exists, it will not be created and returned false
		System.out.println("b1:"+b1);
		boolean b2=java.exists();
		System.out.println("b2:"+b2);
		boolean x1=x.mkdir();
		System.out.println(x1);
		System.out.println(java.isFile());
		System.out.println(a.isDirectory());
 result :
b1:false// Because it has been run once before and has been created, it returns false
b2:true
true
true
true

Effect in folder :
 Insert picture description here

原网站

版权声明
本文为[Aiqing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210545243642.html