Home> Java> javaTutorial> body text

Introduction to java file operations and file filters (with examples)

不言
Release: 2018-09-20 15:08:57
Original
3601 people have browsed it

This article brings you an introduction to Java file operations and file filters (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

File operation

Java provides the File class for us to operate files. FIle is an abstract representation of a file or folder, that is, an object representation. Summarized several commonly used methods

  • File(File parenrt,String child), FIle(String pathname), File(String parent,String child), the three constructors can be used in a variety of ways way to achieve instantiation.

  • getAbsolutePath(): Returns the absolute path of this file or folder

  • getName(): Returns the name of this folder or file

  • getPath(): Returns the representation of this folder or file, which is the parameter in the construction parameter

  • createNewFIle(): Creates a new File

  • mkdir(): Create a new directory, note that only one layer can be created,

  • mkdirs(): Create a multi-layer directory

  • isDirectory(): Determine whether it is a directory

  • isFile(): Determine whether it is a file

  • list(): Returns a string array of names of files or directories

  • listFiles(): Returns an array of file objects of files or directories

File filter:

The listFiles() function returns a list of file objects, and sometimes we only need specific files, such as .txt files, and the rest do not need to be returned, although they can be returned after Then operate the array, but this is a bit too complicated. Java provides us with two interfaces FilenameFilter and FileFilter. Pass this interface object into the parameters of listFiles to achieve the purpose of filtering. The accept in the interface is the filter function. This method is called for each file obtained. If it returns true, it is put into the array. Otherwise, it is removed.

FilenameFilter:

FilenameFilter filter , we can only use the name of the file or directory. The dir parameter is the current object where the file is located, not the FIle object of the file. So we generally just use the name parameter

public interface FilenameFilter { boolean accept(File dir, String name); }
Copy after login

FileFilter:

FileFilter Filter, the parameter pathname is the FIle object of the file, through which we can get all the attributes of the file. For example, pathname.getName() is the name of the file, which can be turned into an indirect FIlenameFilter. Powerful

public interface FileFilter { boolean accept(File pathname); }
Copy after login

Example:

import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; /** * 自定义的局部内部类来自定义过滤器类, */ public class Generics { //自定义过滤器类实现FilenameFilter接口 static class MyFilenameFilter implements FilenameFilter{ @Override public boolean accept(File dir, String name) { return name.endsWith(".java");//只获取.java结尾的文件或目录 } } //自定义过滤器类实现FileFilter接口 static class MyFileFilter implements FileFilter{ @Override public boolean accept(File pathname) { return pathname.getName().endsWith(".java");//只获取.java结尾的文件或目录 } } public static void main(String[] args) { File file =new File("D:"); //方式一:FileFilter过滤器 File[] files=file.listFiles(new MyFileFilter()); for (File f:files){ System.out.println(f.getName()); } //方式二:FilenameFilter过滤器 File[] files2=file.listFiles(new MyFilenameFilter()); for (File f:files2){ System.out.println(f.getName()); } //list()函数只能接受FilenameFilter对象,匿名内部类的形式实现, //或者可以理解为为什么既然有FileFilter,而还要FilenameFilter // 1.对于listFiles来说,FilenameFilter可以直接操作name,而不需要通过File对象获取 // 2.对于list来说,它只能是FilenameFilter过滤器,因为得到的是String,已经不是File了 String[] filenames = file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".java"); } }); for(String str:filenames){ System.out.println(str); } } }
Copy after login

The above is the detailed content of Introduction to java file operations and file filters (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!