Use the Java.io.File class to traverse all files that specify a drive letter or folder on a local disk

Source: Internet
Author: User

2016-11-18

This is my first essay blog, Tangled up a half-day or choose their own learning when the more intentions of some knowledge points. Use the Java.io.File class to specify a local folder to traverse through all the files.

 PackageOrg.lxm.filedemo;ImportJava.io.File;ImportJava.util.Scanner;/** This program is the operation of all folders and files of a certain disk*/ Public classFilealldemo { Public Static voidMain (string[] args) {//interacting with scannerScanner sc =NewScanner (system.in); System.out.println ("Please enter the folder you want to print out: (use" \ \ "under Windows delimiter)"); //receive the input contentString pan =Sc.next (); //instantiating the file classFile File =NewFile (PAN); //calling the Lists methodlists (file); }     Public Static voidlists (file file) {if(File.isdirectory ()) {//determine if a folderfile[] list = File.listfiles ();//use an array to receive a folder with a full path            if(List! =NULL) {                               //Looping through Files                 for(inti = 0; i < list.length; i++) {lists (list[i]); }            }               //Output File nameSystem.out.println (file); }    }}

The above is the Code of implementation.

  FileAn instance of a class is immutable, that is, once created, File the abstract path name represented by the object will never change. Where the path is different from the symbols used by Windows and Linu systems, it is usually delimited with file.separator (constants here), such as file File = new file ("E:" +file.separator+ "1.jpg" );

The frequently used method has the CreateNewFile () method, which indicates that a new file is created, a path must be specified when it is created, for example, if the file above does not exist, it can be created using File.createnewfile (), which throws an exception and requires try{// Create code}catch (IOException e) {} or throws for processing and do not focus here;

The mkdir () method, which represents the Create Folder command, must specify a path as above, such as file File1 = new file ("E:" +file.separator+ "Java"); File1.mkdir () ; A Java folder is created on the specified path;

Delete () method, which means the deletion of the file of the specified path, and the use of File.delete () to delete the file created above;

The exists () method, which indicates whether a file that tests the specified path exists, returns a Boolean value, either True or false, and the use of Test file.exists () after deleting the file will return false, often in an if statement;

The Isfile () method, which indicates whether a file that tests the specified path is a file, returns a true or False,isdirectory () method, indicating whether the specified path is a folder, and uses a little bit;

File[] Listfiles () method, using the array for the specified folder traversal, the usage is to first define a file filepath = new file ("E:" +file.separator+ "folder name"); file[] List = Filepath.listfiles (); Use the enhanced for loop to iterate through the files and folders under the folder name, such as for (File lists:list) {System.out.println (lists);};

The string[] List () method, similar to the above Listfiles () method, is to iterate through the files and folders under the specified folder in the same way that the content that is traversed using the list () method is the root directory: \ filename, such as e:\ file name, The content that is traversed by using the Listfiles () method is the root directory: \ folder name \ file name, such as e:\ folder names \ Filenames. This is a more intuitive distinction;

The above is a common method, please refer to the API for analysis and research.

Use the Java.io.File class to traverse all files that specify a drive letter or folder on a local disk

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.