Implement the file operation command function in Dos in Java

Source: Internet
Author: User

The project needs to be modified over the past few days, mainly by adding the file operation function. For example, after deleting a piece of information, you must also delete the attachment it carries. In the idle time, I decided to expand the function and build a simple version of the DOS command implemented in Java. In Java I/O, class File is used for File operations (I think of them as class for File operations such as InputStream ). Therefore, all the operations we need are implemented through the File class. Next, we will implement the required functions.
I. File Class
1. The class File object describes the File and directory information in the specified path. You can use a File object to view the File and directory information in a specific path and operate on the files and directories in the path.
2. Application of class File
2.1 view the file information in a path.
2.1.1 class File Constructor
Class File has two types of constructor functions. One class has one parameter, which is used to generate the object of the File system in the path to which the description parameter points. The other class has two numbers, the first parameter indicates the path, and the second parameter indicates the subdirectory, which is used to generate the file information of a subdirectory in the path to which the first parameter points. The subdirectory name is specified by the second parameter. For example:
File path = new File ("c: \"); generates an object that describes the File information under c:
File path = new File ("c: \", "test"); generates an object that describes the File information of the test sub-directory of c.
2.1.2 list () and list (FilenameFilter filter) Functions
1) The list () function returns the File information described by a File object in the form of a String array. The returned file names and directory names of all files and directories in the path.
2) The list (FilenameFilter filter) function provides the query function, through which the file name and directory name meeting specific conditions can be queried.
2.1.3 use the FinenameFilter interface to query files or directories
2.1.3.1. this interface only contains one function: boolean accept (File dir, String name). The second parameter represents the name of a File or directory, the first parameter indicates the name of the directory where the file or directory is located. If true is returned, the file meets the query conditions and must be placed in the results. Otherwise, the file does not meet the requirements.
2.1.3.2. Working Principle
When the FilenameFilter filter function of a File object is called, The accept () method in the filter parameter is called for each File or directory in the File object. When the accept () function is called for a File or directory, its name and File object describing its directory are passed as parameters to the accept () function.
2.1.4 a simple instance

  1. ImportJava. io.File;
  2. ImportJava. io.FilenameFilter;
  3. ClassDirList {
  4. Public VoidPrintFile (StringDir,StringFilter ){
  5. FilePath =New File(Dir );
  6. String[] FileList;
  7. If(Filter.Length() = 0 ){
  8. System. Out. println ("All file :");
  9. FileList = path. list ();// Obtain all file information
  10. }
  11. Else{
  12. System. Out. println ("All file including" + filter );
  13. // Obtain the file information that meets the query Conditions
  14. FileList = path. list (NewDirFilter (filter ));
  15. }
  16. For(IntI = 0; I <fileList.Length; I ++)
  17. System. Out. println (fileList [I]);
  18. }
  19. Public VoidPrintFile (StringDir ){
  20. PrintFile (dir ,"");
  21. }
  22. }
  23. ClassDirFilterImplements FilenameFilter{
  24. StringAfn;// Store query Conditions
  25. DirFilter (StringAfn ){This. Afn = afn ;}
  26. // Returns true if the query conditions are met.
  27. Public BooleanAccept (FileDir,StringName ){
  28. ReturnName. indexOf (afn )! =-1;
  29. }
  30. }
  31. Public ClassTestIO {
  32. Public Static VoidMain (String[] Args ){
  33. DirList dirList =NewDirList ();
  34. // Display the file information under F: est
  35. DirLis

Related Article

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.