Test some methods of the file class

Source: Internet
Author: User
Tags netbeans

Description of the file class
First, create a new project filetext in netbeans. Open the folder where the project is located and you will find that netbeans automatically created
Build, nbproject, SRC, test folder, and build. XML, mainffest. MF File
The following uses these files to test the usage of the file class.
1) observe its constructor file (string pathname)
// Here "." refers to reading files (folders) in the filetext folder)
File file = new file ("."); // where new file ("") is also located in the filetext File
---------------------------------------------------------------------
2) // obtain the absolute path and print the output as D: \ programe \ netbeansprojects \ thinkinjava \.
System. Out. println (file. getabsolutepath ());
---------------------------------------------------------------------
3) observe the tostring () method of the file source code. This method returns a string path attribute of file.
Therefore, when the output system. Out. println (File) is ".",
-------------------------------------------------------------------
4) obtain all files (folders) in the filetext directory)
String list [] = file. List ();
Print the list cyclically: Build, build. XML, manifest. MF, nbproject, SRC, test
-------------------------------------------------------------------
5) obtain the files (folders) whose names meet certain conditions in the filetext directory.
List (filenamefilter filter) method, where filenamefilter is an interface that provides unique
One Method

// This method mainly tests whether the specified file should be included in a file list.
Accept (File Dir, string name );
For example, here I want to find the file named build (folder ).
Implement the filenamefilter interface in the anonymous class
The Code is as follows:
String [] list = path. List (New filenamefilter (){
// Obtain the file named build (folder)
Public Boolean accept (File Dir, string name ){
If (name. Equals ("build ")){
Return true;
}
Return false;
}
});
In fact, in actual design, you can use regular expressions to implement more complex filtering, which is only used for testing.
To understand how the filter works, you can view the list (filenamefilter filter) in the file class)
The specific implementation is as follows:

Public String [] list (filenamefilter filter) {// use the list () method of this class to obtain the string Names [] = List () of all files in the specified directory (); // If names is null or filter is null, an empty array names if (names = NULL) | (filter = NULL) {return names;} is returned ;} // define an arraylist to store the required files. arraylist v = new arraylist (); // traverse names cyclically to see if any files meet the requirements (folder) for (INT I = 0; I <names. length; I ++) {// determines whether the current file name meets the requirements. If yes, add If (filter. accept (this, Names [I]) {v. add (Names [I]) ;}// return the file (folder) Return (string []) (V. toarray (New String [v. size ()]);}

Note that filternamefilter is only an interface, and there is no specific implementation in the API. The accept method is specific.
The implementation is completely set by programmers and flexible.
----------------------------------------------------------------------------------
Listfiles (filefilter filter)
Returns an array of abstract path names indicating files and directories in the directory. These paths meet the requirements of specific filters.
The following uses this class to obtain the file name with a specific suffix.

Class myfilefilter extends filefilter {// used to store the file suffix string [] suffarr that can accept; // the parameters of the constructor method are the file types we need to filter. For example, the Excel file is xls, And the EXE file is exe public myfilefilter (string [] strings) {This. suffarr = strings;} public myfilefilter () {super () ;}// implement the filtering method public Boolean accept (File file) {// check whether the filename suffix is in suffarr for (string S: suffarr) if (file. getname (). endswith (s) return true; return file. isdirectory ();}

Therefore, you can use the following code to obtain images in a specific format.
Myfilefilter MFF = new myfilefilter (New String [] {". BMP ",". jpg ",". JPEG ",". jpe ",". jfif ",". GIF ",". TIF ",
". Tiff", ". PNG", ". ICO "})
File [] files = file. listfiles (MFF );
Like the list (filenamefilter filter) method, you can clearly see how it is implemented by viewing the source code.

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.