java operations on files
As for the reading and writing of files, Xiao Yuan does not follow the usual article framework to write this article. Xiao Yuan writes the notes of this chapter based on cases.
File creation
The creation of a file is relatively simple. Using File file = new File("xxx") can create a folder or a file. I won't do too much discussion on the content of this summary.
File deletion
For the deletion of files, Xiao Yuan directly presents a case, using java code to directly delete the test files created by Xiao Yuan.
Test code
public class Demo01Test {
public static void main(String[] args) {
File file = new File("G:\\test");
deleteAllFile(file);
}
public static void deleteAllFile(File file){
System.out.println("-----------------------");
File[] files = file.listFiles();
for (File file1 : files) {
if(file1.isDirectory()){
System.out.println("delete file"+file1);
deleteAllFile(file1);
}else{
file1.delete();
}
}
System.out.println("delete directory:"+file);
file.delete();
System.out.println("-------------------------");
}
}
Test Results
File search
The functional requirement of case one is to display all folders, and the code implementation is shown below.
public class Demo04Recurison {
public static void main(String[] args) {
File file = new File("G:\\test");
getAllFile(file);
}
public static void getAllFile(File dir){
System.out.println(dir);
File[] files = dir.listFiles();
for (File f :
files) {
if (f.isDirectory()){
getAllFile(f);
}else {
System.out.println(f);
}
}
}
}
Test results The
test results show that all searched files and folders have no problems.
Case two
The requirement of case two is to output all files ending in .txt. First upload the code, as shown below.
public class Demo04Recurison {
public static void main(String[] args) {
File file = new File("G:\\test");
findAllFiles(file);
}
public static void findAllFiles(File dir){
File[] files = dir.listFiles();
for (File f :
files) {
if (f.isDirectory()) {
findAllFiles(f);
}else{
if(f.toString().toLowerCase().endsWith(".txt")){
System.out.println(f);
}
}
}
}
}
All in all, the overall idea of file search is to make conditional judgments on the target file when traversing the file recursively, so as to select the target file.
For search file filters, the disadvantage of the above code is that once the code is formed, it means that the function of the code is fixed. If you want to realize different functions, you must rewrite the code. If you want to change this situation, you must use the interface to search for files. The function defines a fixed template,
Case three
Use FileFilter filter to achieve file search, FileFilter source code is as follows.
@FunctionalInterface
public interface FileFilter {
/**
* Tests whether or not the specified abstract pathname should be
* included in a pathname list.
*
* @param pathname The abstract pathname to be tested
* @return <code>true</code> if and only if <code>pathname</code>
* should be included
*/
boolean accept(File pathname);
}
note, Here is why there is no abstract class for the interface function, because the interface defaults to adding an abstract function, and look at the following code.
Therefore, the word abstract is not seen in FileFilter. Below, Xiaoyan will use anonymous inner classes and lambda expressions to implement file filtering case
method one (anonymous inner class)
directly on the code. First, write the FileFilter interface implementation class.
public class FileFilterImpl implements FileFilter{
@Override
public boolean accept(File pathname) {
/*
过滤的规则:
在accept方法中,判断File对象是否是以.java结尾
是就返回true
不是就返回false
*/
//如果pathname是一个文件夹,返回true,继续遍历这个文件夹
if(pathname.isDirectory()){
return true;
}
return pathname.getName().toLowerCase().endsWith(".java");
}
}
Test class
public class Demo01Filter {
public static void main(String[] args) {
File file = new File("c:\\abc");
getAllFile(file);
}
public static void getAllFile(File dir){
File[] files = dir.listFiles(new FileFilterImpl());//传递过滤器对象
for (File f : files) {
if(f.isDirectory()){
getAllFile(f);
}else{
System.out.println(f);
}
}
}
}
Method two (Lambda expression implementation)
In order to review and consolidate the previous lesson, lambda expressions are used. Method two will change the interface implementation class into lambda expressions
public class Demo02Test {
public static void main(String[] args) {
File file = new File("G:\\test");
getTargetFile(file);
}
public static void getTargetFile(File dir){
//采用匿名内部类的方式
File [] files =null;
/*files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory()||pathname.getName().toLowerCase().endsWith("java");
}
});
//采用lambda表达式。
files=dir.listFiles((File pathname)->{
return pathname.isDirectory()||pathname.getName().toLowerCase().endsWith("java");
});*/
//优化lambda 表达式
files=dir.listFiles((pathname)-> pathname.isDirectory()||pathname.getName().toLowerCase().endsWith("txt"));
for (File file :
files) {
if (file.isDirectory()){
getTargetFile(file);
}else {
System.out.println(file);
}
}
}
}
Test result:
So far, the related operations of the file have been reviewed. The next chapter will review the operations related to the file flow.