The use of file class and recursive thought File class overview
Files: Storing data
Folders: Managing Files and folders
Construction method
- Public file (String pathname): Creates a new File instance by converting the given pathname string to an abstract path name.
- Public file (string parent, String child): Creates a new File instance from the parent pathname string and the subpath name string.
- Public file (file parent, String child): Creates a new File instance from the parent abstract path name and subpath name string.
Common Method Creation methods
- public boolean createnewfile (): Create file, Presence returned false, no create file returned true
- Boolean mkdir (): Create folder, Existence returns False, does not exist Create folder returns True
- Boolean mkdirs (): Create multi-level folder, Existence returns False, does not exist create multilevel folder returns True
Delete method
- Boolean Delete (): Delete file or folder, file exists return true, file does not exist return false;
Can delete empty folder, if there are sub-files or subfolders under the folder, delete
Judging method
- public Boolean exists (): The file or directory that this file represents is actually present.
- public boolean isdirectory (): This file indicates whether it is a directory (folder).
- public boolean isfile (): This file indicates whether the files are.
Path problem
Absolute path: Starts with a drive letter
Relative path: relative to this item
- public string GetAbsolutePath (): Returns the absolute path name string for this file.
- Public String getParent (): Gets the parent path, the parent path is established when the object is created, and no return null is specified (relative path returns null).
- public string GetPath (): Converts this file to a pathname string (gets the path at build time).
- Public String getName (): Returns the name of the file or directory represented by this file.
- public long Length (): Returns the length of the file represented by this file.
Advanced methods
- Public string[] List (): Returns a String array that represents all the child files or directories in the file directory.
- Public file[] Listfiles (): Returns a file array that represents all the sub-files or directories in the file directory.
Recursive overview
Recursion is the method inside itself that calls itself.
Precautions
- There must be an exit, otherwise the stack memory overflow error will occur.
- Recursion should not be too many times. Otherwise prone to stack memory overflow
- Construction method cannot be recursive
Understand
For recursion, you can use the stack to understand, into the stack and out of the stack, for the whole process of the program, first, the main method into the stack, and then recursive method into the stack, the method calls itself,
Continue into the stack, know the exit to return to the stack, layer out of the stack, this is the underlying principle of it.
Calculates the factorial of n
import java.util.Scanner;/** * 求n阶乘 * 1.手动输入计算的值 * 2.创建递归方法进行计算 * @author WZLOVE * @create 2018-07-20 14:34 */public class Demo1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入您要计算的值是:"); int n = in.nextInt(); in.close(); long result = getRecursive(n); System.out.println("您输入的值的阶乘计算结果为:" + result); } public static long getRecursive(int n){ // 定义出口 if( n == 1 ){ return 1; } return n * getRecursive( n - 1 ); }}
Print multi-level catalogs
Package Com.wzlove.recursive;import java.io.file;/** * Traverse Multilevel Directory * * @author wzlove * @create 2018-07-20 15:11 */public class Demo2 {public static void main (string[] args) {//Program robustness Test//File file = null; File File = new file ("e:\\"); Test path File File = new file ("g:\\ work interval \\Intellij idea\\javase"); Printdirsandfiles (file); public static void Printdirsandfiles (file file) {//program robustness if (file = = NULL | |!file.exists ()) { return; }//If it is a file, direct output if (File.isfile ()) {System.out.println (File.getabsolutepath ()); }//If it is a folder, traverse recursive file[] files = file.listfiles (); If there is no access permission, return directly if (files = = null) {return; } for (File file1:files) {if (File1.isfile ()) {System.out.println (File1.getabsolutepath ()); } else {System.out.println (File1.getabsolutepath ()); PrintdirsaNdfiles (FILE1); } } }}
Print all. java files in a directory
Two methods, the first type:
Package Com.wzlove.recursive;import java.io.file;/** * search for. java files in the G:\ workspace \intellij idea\javase directory. * 1. Create an object of file, passing the virtual path of file * 2. Recursive method * 3. Make a robust judgment: * * NULL Judgment * * file does not exist * * If the path is a file, these three cases are returned directly * * AS The fruit path is valid * (1) to determine if there is no permission, there is continued downward, no direct return * (2) returns all files and folders under the folder * (3) Enhanced for loop traversal * If it is a file, judge Whether it satisfies the requirement (recursive exit) * If it is a folder, make a recursive call * * @author wzlove * @create 2018-07-20 16:27 */public class Demo3 {public static void Main (string[] args) {File file = new file ("g:\\ working interval \\Intellij idea\\javase"); Printalljavafiles (file); public static void Printalljavafiles (file file) {if (file = = NULL | |!file.exists () | | file.isfile ()) { return; } file[] files = file.listfiles (); if (files = = null) {return; } for (File file1:files) {if (File1.isfile ()) {if (File1.getname (). EndsWith (". Java")) { System.out.println (File1.GetAbsolutePath ()); }}else{Printalljavafiles (file1); } } }}
Second, to use the file filter:
-
File[] Listfiles (filefilter filter) returns an abstract path an array group that represents the files and directories in the directory that satisfy the specified filter as represented by this abstract path name.
Package com.wzlove.recursive; Import Java.io.File; Import Java.io.FileFilter; /** * * @author wzlove * @create 2018-07-20 16:27 */public class Demo4 {public static void main (string[] A RGS) {File File = new file ("g:\\ work interval \\Intellij idea\\ podcast \\JavaSE"); Printalljavafiles (file); public static void Printalljavafiles (file file) {if (file = = NULL | |!file.exists () | | file.isfile ()) { return; }//Using Listfiles (filefilter)/** * file[] Listfiles (filefilter filter) * Returns an abstract path an array group That represents the files and directories in the directory that satisfy the specified filter, represented by this abstract path name. */file[] files = file.listfiles (new FileFilter () {@Override public boolean accept (Fil E pathname) {if (Pathname.isfile () && pathname.getname (). EndsWith (". Java")) { Is the file that ends with a. java file return true; }//If it is a folder, also return if (Pathname.isdirectory ()) {return true; }//The remaining conditions return false return false; } }); for (File file1:files) {if (File1.isfile ()) {System.out.println (File1.getabsolutepath ()); }else{printalljavafiles (FILE1); } } } }
Java file and recursion