java IO computer search delete function of the instance
- 2020-05-26 09:13:14
- OfStack
1. Recursive methods
1. Recursion is calling its own methods, provided there are methods.
2. Recursive use
Find the pattern of recursion
Recursion has to have an exit condition, which is the end condition
3. Precautions
Recursion should not be too many times, otherwise stack overflow will occur
Recursion cannot be nested, otherwise dead recursion occurs
2. IO is introduced
1. i is the input of Input, O is the output of Output, and API encapsulates the input and output streams into a single class, providing a large number of methods for us to use.
2. IO technology can write data to the persistent device (including hard disk, optical disk, U disk, etc.). Although collections and arrays can be stored and read, they are only temporarily stored in memory and will not exist when the program is restarted.
3. Relative to the reference of memory, the things on the hard disk are read into memory, which is called the input stream. Writing data from memory to the hard disk is called an output stream.
3. The code realizes the function of computer search
1. Search for folders with custom suffixes
package cn.jasonFile01;
import java.io.File;
/**
* @author Jason
* @2016 years 9 month 18 day In the afternoon 9:12:35
*/
public class FindFiles04 {
// Clever use of member variables
// Statistical variables can be placed in the member location
static int countFile = 0;
public static void main(String[] args) {
// Encapsulates the parent class file object
File parent = new File("F:\\API");
// Define the suffix name of the file to look for
String suffix = ".pdf";
// The method to find the file is called
scanFiles(parent, suffix);
// Output the number of files that meet the criteria
System.out.println(" There are documents that meet the criteria " + countFile + " a ");
}
/**
* This is looking for eligible files
* 1. Gets all the subfiles in the current directory and their subdirectories
* 2. Iterate through the groups to see if they are files
* Is: in the case of suffixes, the absolute path of the direct output file , Counter to add 1
* No: then recurse
* Note: if it is a file ( Not a directory ) call listFiles Method, then what is returned is null
*/
private static void scanFiles(File parent, String suffix) {
// Gets all the subfiles in the current directory and their subdirectories
File[] files = parent.listFiles();
// Prevents files that do not have access on the system from returning null value
if (files != null)
for (File file : files) {
if (file.isFile() && file.getName().endsWith(suffix)) {
System.out.println(file.getAbsolutePath());
countFile++;
} else
scanFiles(file, suffix);
}
}
}
2. Search for folders with custom suffixes
package cn.jasonFile01;
import java.io.File;
/**
* @author Jason
* @2016 years 9 month 18 day In the afternoon 9:12:20
*/
public class FindFolder {
// Define statistical variables
static int countFolder = 0;
public static void main(String[] args) {
// Encapsulates the parent class file object
File parent = new File("F:\\a");
// Define the suffix name of the file to look for
String suffix = "1";
// The find folder method is called
scanFolder(parent, suffix);
// Output the number of directories that meet the criteria
System.out.println(" There are folders that meet the criteria " + countFolder + " a ");
}
/**
* This is the way to find the number of folders that satisfy the criteria
* 1. Gets all the subfiles in the current directory and their subdirectories
* 2. Traversal group to determine if it is a directory
* Is: then recursion
* No: leave it alone
* 3. Output absolute path with the same suffix name
* Note: if it is a file ( Not a directory ) call listFiles Method, then what is returned is null
*/
private static void scanFolder(File parent, String suffix) {
// Gets all child directories and child files in the parent directory
File[] files = parent.listFiles();
if (files != null)
// Go through the groups to make a judgment
for (File file : files) {
if (file.isDirectory())
scanFolder(file, suffix);
}
// Rent-free conditions for directory output
if (parent.getName().endsWith(suffix)) {
System.out.println(parent.getAbsolutePath());
countFolder++;
}
}
}
4. The code to achieve the complete deletion of the computer function
Note: Java is deleted without recycling. Please use it carefully.
package cn.jasonFile01;
import java.io.File;
public class ClearAll {
public static void main(String[] args) {
// Encapsulates the parent directory as file object
File parent = new File("F:\\IO Modify file name ");
// Calling the delete method
clearAll(parent);
}
/**
* This is the method to delete all child directories and files in the parent directory
* 1. Gets all the child files and subdirectories in the parent directory
* 2. Variable array and determine if it is a file
* deletes
* No: recursion
* 3. Until subfiles in the directory are deleted, then delete the empty file
*/
private static void clearAll(File parent) {
// Gets all the child files and subdirectories in the parent directory
File[] files = parent.listFiles();
//1. Prevent system files from not being located and null pointer exception occurs
//2. To prevent system path from not existing causes the array to be null , a null pointer exception occurred
if (files != null)
// Through the array
for (File file : files) {
if (file.isFile())
file.delete();
else
clearAll(file);
}
// Delete empty folder after deleting files
parent.delete();
}
}