IO (File recursion), iofile Recursion

Source: Internet
Author: User
Tags file separator

IO (File recursion), iofile Recursion

File Overview
Java. io. File class: abstract representation of File and directory path names.
Used to describe files, folders, and paths on a computer

Commonly used three File-related words:
File: file
Directory: folder (directory)
Path: path
File is a system-independent class.

Three overloaded construction methods of the File class
Path:
The directory Separator of the window system is \
The directory Separator in java is \ or/
Path Classification:
Absolute path: Path starting with a drive letter
Example: D: \ ase \ 20170514 \ day10
D: \ Work_EE_266 \ day10 \ src
Relative Path:Compared with the current project, the path between the drive letter and the project can be omitted during path writing.
D: \ Work_EE_266 \ day10 --> src

Note: lujin is case insensitive.
File (File parent, String child)
Transfer Path: the parent path of the File type and the sub-path of the string type.
Benefit: the parent path is of the File type. The parent path can directly call the method of the File class.
File (String parent, String child)
Transfer Path: the parent path of the string type and the Child path of the string type.
Benefits: You can use the parent path and sub-path separately and use the path as a parameter.
File (String pathname)
Pass path name: you can write a folder or a file.
C: \ abc \ Demo. java
The path can be created if it does not exist. The path is case insensitive.

1 public static void main (String [] args) {2/* 3 * static String pathSeparator is a system-related path separator. For convenience, it is represented as a String. 4 * static char pathSeparatorChar: system-related path separator. 5 * static String separator is the default name separator related to the system. For convenience, it is represented as a String. 6 * static char separatorChar default name separator related to the system. 7 */8 String pathSeparator = File. pathSeparator; 9 System. out. println (pathSeparator); // path separator windows semicolon; linux Colon: 10 11 String separator = File. separator; 12 System. out. println (separator); // directory name delimiter windows backslash \ linux forward slash/13 14/* 15 * methods in the System class 16 * static String getProperty (String key) obtains the system attributes of the specified key indication. 17 * file. separator file separator ("/" in UNIX System) 18 * path. separator path separator (":" in UNIX systems) 19 * line. separator line separator ("/n" in UNIX) 20 */21 System. out. println (System. getProperty ("file. separator "); 22 System. out. println (System. getProperty ("line. separator "); 23 System. out. println (System. getProperty ("path. separator "); 24}

File class creation and Deletion
Delete File
Boolean delete ()
Delete a File or folder, which is provided in the File constructor.
If the deletion is successful, true is returned. If the deletion fails, false is returned (NO content exists and the folder contains content)
The deletion method does not go to the recycle bin and is directly deleted from the hard disk.
Deletion is risky, so proceed with caution


File Folder creation Function
Boolean mkdir () can only create single-layer folders
Boolean mkdirs () can create both single-layer folders and multi-layer folders.
The created path is also given in the File constructor.
If the folder already exists, it is not created


File Creation Function
Boolean createNewFile ()
The path and File name of the created File are provided in the File constructor.
If the file already exists and is not created, false is returned.
Only files can be created, but folders cannot be created (view type, not suffix)
The path of the created folder must exist.

File class judgment Function
Boolean isDirectory ()
Determine whether the path encapsulated in the File constructor is a folder
If it is a folder, true is returned. If it is not a folder, false is returned.
Boolean isFile ()
Determine whether the path encapsulated in the File constructor is a File
Boolean exists ()
Determine whether the encapsulated path in the File constructor exists
If yes, true is returned. If no, false is returned.

File class Acquisition Function
String getParent () returns the String object
File getParentFile () returns the File object
Returns the parent path at the end of the file.

Long length ()
The number of bytes of the file in the returned path. The folder is not in size.

String getPath () converts the abstract path name to a path name String.
Same as toString

String getName ()
The name of the file or folder in the returned path.
Get the name of the last part of the path

File getAbsoluteFile () returns the absolute path name form of this abstract path name.
String getAbsolutePath () returns the absolute path name String of this abstract path name.
Obtain absolute path


ListFiles
Note:
1. Only one directory can be traversed.
2. The directory to be traversed must exist.
Otherwise, a null pointer exception will be thrown.

Static File [] listRoots ()
Obtain all root directories in the system

File [] listFiles ()
Get the File and folder names in the path encapsulated in the File Constructor (traverse a directory)
Returns the full path of a directory or file.

String [] list ()
Get the File and folder names in the path encapsulated in the File Constructor (traverse a directory)
Only the name is returned.

Recursion
Recursion: The method calls itself.
Category:
There are two types of recursion: Direct recursion and indirect recursion.
Direct recursion is called the method itself calling itself. Indirect recursion can call method A to call Method B, Method B to call method C, and method C to call method.

Note:
1. recursion must be conditional so that recursion can be stopped. Otherwise, stack memory overflow may occur.
2. There are limits in recursion, but the number of recursion times cannot be too many. Otherwise, stack memory overflow occurs.
3. constructor to disable recursion

1 using recursion between 1-n and 2 n + (n-1) + (n-2) + (n-3) +... + 1 3 5 + (5-1) + (4-1) + (3-1) + (2-1) 4 end condition: end when n = 1 5 method self-call purpose: Get n-1, get n = 1 end 6 public static int DGSum (int n) {7 // Add end Condition 8 if (n = 1) {9 return 1; 10} 11 return n + DGSum (n-1 ); 12} 13 14 use recursion to calculate the factorial 15 private static long DGJC (int n) {16 // recursion end condition n = 117 if (n = 1) {18 return 1; 19} 20 return n * DGJC (n-1); 21} 22 23 use recursion to calculate the Fibonacci series 24 private static int fbnq (int month) {25 // The end condition returns 126 if (month = 1 | month = 2) {27 return 1; 28} 29 // more than if month is: the number of Rabbits is the sum of the first two months. 30 return fbnq (month-1) + fbnq (month-2); 31}

File Filter
File filter:
Requirement: traverse the hello folder and retrieve only the files ending with. java in the folder
C: \ hello
C: \ hello \ demo.txt
C: \ hello \ Hello. java

In the File class, the listFiles () method is used to traverse folders.
There are two methods that are overloaded with listFiles. The parameter is passed as a filter.
File [] listFiles (FileFilter filter)
File [] listFiles (FilenameFilter filter)
Returns an array of abstract path names, which indicate the files and directories that meet the specified filter conditions in the directories indicated by this abstract path name.
The FileFilter and FilenameFilter parameters of the method are found to be interfaces.
We need to define the implementation class of the interface, override the method accept in the interface, and implement the filter function.

1 public class FileFilterImpl implements FileFilter {2/* 3 * filtering method: 4 1. change the passed path pathname to the string 5 Stirng s = pathname. toString (); "c: \ hello \ demo.txt" 6 String s = pathname. getPaht (); "c: \ hello \ demo.txt" 7 String s = pathname. getName (); "demo.txt" 8 2. use the method endsWith in the String class to determine whether the String ends with the specified String 9 boolean B = s. endsWith (". java "); 10 return B; 11 */12 @ Override13 public boolean accept (File pathname) {14/* String s = pathname. getName (); 15 boolean B = s. endsWith (". java "); 16 return B; */17 return pathname. getName (). toLowerCase (). endsWith (". java "); 18} 19} 20 public class FilenameFilterImpl implements FilenameFilter {21 22 @ Override23 public boolean accept (File dir, String name) {24 return name. toUpperCase (). endsWith (". JAVA "); 25} 26 27}

Breakpoint debugging
Debug breakpoint debugging
F6: run the command line by line.
F5: Enter the Method
F7: End Method
F8: jump to the next breakpoint
Watch: Capture



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.