Java--file class, Recursive

Source: Internet
Author: User

first, the file classget path or directory separator
public static void Main (string[] args) {//file class static member variable//system-related path delimiter String separator = file.pathseparator; SYSTEM.OUT.PRINTLN (separator);//is a semicolon, directory partition of   Linux://System-related default name delimiter separator = File.separator; SYSTEM.OUT.PRINTLN (separator);//right \  directory name split  Linux/}
1, 3 kinds of construction methods
/* *  file (file parent,string child) *  pass path, pass file type parent path, string subpath *  Benefit: Parent path is file type, parent path can call file class method directly */public static void Function_2 () {File parent = new File ("D:"); File File = new file (parent, "Eclipse"); System.out.println (file);} /* *  File (string parent,string Child) *  pass path, pass string parent path, string subpath *  benefit: Operation parent and subpath separately */public static void function _1 () {File File = new file ("D:", "Eclipse"); System.out.println (file);} /* *  file (String pathname) *  Pass path name: can write to a folder, can write to a file *  c:\\abc   C:\\abc\\demo.java *  Encapsulates a path to the file type object */public static void function () {File File = new file ("D:\\eclipse"); System.out.println (file);}
2. Create a file
/* * File  creation function *  boolean createnewfile () *  creates a file path and file name, given in the file constructor method *  file already exists, no longer created, return value is false * /public static void function () throws Ioexception{file file = new file ("C:\\a.txt"); Boolean B = File.createnewfile (); System.out.println (b);}
3. Create a folder
/* *  File Create folder function *  boolean mkdirs () create a multi-tiered folder * The  path created is also given in the file construction method *  folder already exists, no longer created, and the return value is False */public static void Function_1 () {File File = new file ("C:\\abc"); Boolean B = File.mkdirs (); System.out.println (b);}
4. Delete files and folders
/* *  the Delete function of the file class *  Boolean Delete () * Delete  the file or the folder, given in the file constructor method *  Delete succeeded return true, delete failed return false *  Delete method, Do not walk the Recycle Bin, remove directly from the hard drive *  Delete risky, operation need to be cautious */public static void Function_2 () {File File = new file ("C:\\a.txt"); Boolean B = File.del Ete (); System.out.println (b);}
5. Get the name of the file or folder
/* *  File class get function *  String getName () *  returns the filename represented in the path or folder name * Gets the name of the  last part of the path */public static void function () { File File = new file ("D:\\eclipse\\eclipse.exe"); String name = File.getname (); SYSTEM.OUT.PRINTLN (name);}
6. Get the number of bytes in the file
/* * File class get function * long length () * Returns the number of bytes of the file represented in the path */public static void Function_1 () {File File = new file ("D:\\eclipse\\eclip Se.exe "); Long length = File.length (); System.out.println (length);}
7. Get the absolute path
/* * File class Get function * String GetAbsolutePath () returns a String Object * file   getabsolutefile () returns the file object */public static void Function_2 () {File File = new file ("src"); File absolute = File.getabsolutefile (); System.out.println (absolute);}
8. Get the parent path
/* * File class Get function * String getParent () return string Object * file Getparentfile () return file Object * Get parent path */public static void Function_3 () {F ile file = new file ("D:\\eclipse\\eclipse.exe"); File parent = File.getparentfile (); SYSTEM.OUT.PRINTLN (parent);}
9. Determine if the path exists
/* *  file Judgment function *  Boolean exists () *  determine if the package path exists in the file construction method *  exists returns true, there is no return false */public static void function () {File File = new file ("src"); Boolean B = file.exists (); System.out.println (b);}
10. Decide whether to file/folder
/* *  file Judgment function *  boolean isdirectory () *  determines whether the path encapsulated in the file construction method is not a folder *  if it is a folder, returns True, not the file returns false *   *  boolean isfile () *  determines whether the path encapsulated in the file construction method is not a */public static void Function_1 () () {filename = new file ("d:\\eclipse\\ Eclipse.exe "), if (File.exists ()) {Boolean b = File.isdirectory (); System.out.println (b);}}
11. Traversing all the files in the directory
/* *  the FETCH function of the file class *  file[] listfiles () *  gets the file and folder name (traversing a directory) in the path encapsulated in the file construction method *  Returns the full path of the directory or file */ public static void Function_1 () {File File = new file ("D:\\eclipse"); file[] Filearr = File.listfiles (); for (file F:filearr) {System.out.println (f);}} /* *  the FETCH function of the file class *  string[] List () *  gets the file and folder name (traversing a directory) in the path encapsulated in the file construction method *  returns only the name */public static void function () {File File = new file ("C:"); string[] Strarr = File.list (); System.out.println (strarr.length); for (String Str:strarr) {System.out.println (str);}}
12. File Filter

We need to write a filter ourselves.

Import Java.io.file;import java.io.filefilter;/* *  Custom filter *  implement FileFilter interface, override abstract method */public class Myfilter Implements Filefilter{public Boolean accept (file pathname)  {/* * pathname is also the full path of the file * C:\\demo\\1.txt * To determine the path, if Is a Java file that returns True, not a Java file, and returns false * The suffix end of the file is. java *///string name = Pathname.getname (); return Pathname.getname (). EndsWith (". Java");}}

Then add its anonymous object to the Listfiles

Import java.io.file;/* * File class, get  filter *  When you traverse the directory, you can only get the file that satisfies the condition if necessary *  Traverse directory Method Listfiles () overloaded form *  Listfiles (filefilter Filter) interface type *  transitive FileFilter Interface implementation class *  Custom FileFilter Interface implementation class, overriding abstract method, *  Interface implementation Class object passed to Traverse method Listfiles */public class FileDemo1 {public static void main (string[] args) {File file = new file ("C:\\demo") ; file[] Filearr = file.listfiles (New Myfilter ()); for (file F:filearr) {System.out.println (f);}}}
Second, recursion

Example:

/* * Method  Recursive Call *    method itself calls itself * suitable for, the method of the principal of the  operation is not changed, but when running, the parameters of the participating operation will change *  Note: *     recursive must have an exit, you must be able to stop the program *     Recursive times cannot be too much *     construction method, prohibit recursive */public class Diguidemo {public static void main (string[] args) {/*int sum = getsum (3); SYSTEM.OUT.PRINTLN (sum); */system.out.println (Getjiecheng (5)); System.out.println (GETFBNQ (12));}  /*  method recursive, computes Fibonacci sequence *   */public static int getfbnq (int month) {if (month = = 1) return 1;if (month = = 2) return 1;return GETFBNQ (month-1) +getfbnq (month-2);} /  * *  calculate factorial 5! *   5*4*3*2*1 */public static int Getjiecheng (int n) {if (n = = 1) return 1;return n * Getjiecheng (n-1);} /* *  calculate 1+2+3+100 and = 5050 *  Calculation rule: *    n+ (n-1) + (n-2) *    100+ (100-1) + (99-1) + ... 1 */public static int getsum (int n) {if (n = = 1) return 1;return n + getsum (n-1);}}

If we want to traverse all the files in the directory, we can use this method

/* *  defines the method that implements the full traversal of the directory */public static void Getalldir (File dir) {System.out.println (dir);//Call Method Listfiles () to the directory, Dir to traverse file[] Filearr = Dir.listfiles (); for (File F:filearr) {///Determine the path of the variable F is not a folder if (F.isdirectory ()) {//is a directory, Going to traverse this directory//This method, Getalldir, is to give a directory to traverse//continue to call Getalldir, pass his directory getalldir (f);} Else{system.out.println (f);}}

Java--file class, Recursive

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.