The file record of the Java class

Source: Internet
Author: User

To do a file read write, you need to use a class: file, here is a simple record.

Constructs the method, passes in a string, the absolute path and the relative path all can, the file name or the folder also can:

 Public File (String pathname)
There are three other methods, but personally think that the usefulness is not very good, not the table. The way to use it is new one:
New File (FileName);  

Special note is that here is a file object, really just an object, and your computer on the actual file, not a half-dime relationship, it is just an ordinary object, holding the file name, and then parse out the other point, such as file name, file system type Ah, delimiter what.

Provides some other ways to use it:

1, get the filename, if it is a file, is the filename, if it is a folder, that is the folder name? If it is in the form of "./test.txt", the result is test.txt, if this "./" is passed, then it is directly "." The

 Public String GetName ()

2. Get the parent file name:

 Public String getParent ()

3. Get Superior Documents:

 Public File Getparentfile ()

4. Determine if it is an absolute path:

 Public boolean Isabsolute ()

5. Obtain the corresponding absolute path:

 Public String GetAbsolutePath ()

6. Get a file instance in absolute path form:

 Public File Getabsolutefile ()

7, get the path, this path is the standard format of the path, for win, will return can be directly used to put in the folder address bar open:

 Public String Getcanonicalpath ()

This method is a bit different from the above two methods, it is not easy to express, a picture illustrates the difference:

8. Get the file instance corresponding to the address of the method above:

 Public File Getcanonicalfile ()

9, still can touri, also do not know when useful oh:

 Public URI Touri ()

10, to determine whether this file instance corresponding to the true document is readable, this is somewhat used:

 Public boolean canRead ()

11, there is a readable judgment can be written to judge naturally:

 Public boolean canWrite ()

12. Whether there is any judgment:

 Public Boolean exists ()

13. Determine if it is a folder? is the path? Is it a catalogue? That's not the file anyway:

 Public boolean isdirectory ()

14. Judge whether it is a file:

 Public boolean isfile ()

15, can also determine whether it is hidden files:

 Public boolean Ishidden ()

16, last modified time, return a long form of the number ... :

 Public long lastmodified ()

17, file length, returns a long number, indicating the number of bytes in the actual file (byte):

 Public long Length ()

Where do you use it? When reading the file, if you want to read it all, such as a text file of a moderate size, want the entire read out, you need to prepare a byte array, how much better array length? This is not the length of it:

byte[] filecontent = new Byte[filelength.intvalue ()];  

18, you can also create a new file, of course, only the file does not exist to create and return True, if the file exists and does not create and return false:

 Public boolean createnewfile ()

19, delete files, this and the actual file on the machine hooked up:

 Public boolean Delete ()

20. Delete the files when the JVM exits??? :

 Public void deleteonexit ()

From the most common main method, when the main method exits, the file is deleted, and the Web project is not determined when the delete operation is made.

21. Get all the file names under the current path (if it is a path):

 Public String[] List ()

If it is not a path but an ordinary file, the method returns NULL.

If it is a path, there is no guarantee of the order of the filenames in the returned array, which is said to be especially not to be returned in alphabetical order. I found in the test in the win seems to be in order to return, but the order of this thing, it told us not to believe, then we think it is disorderly order it, really want to sort on the line.

22. Get an array of file instances for all filenames under the current path:

 Public File[] Listfiles (filenamefilter filter)

The filenamefilter here is just an interface, which can be used in this way:

Pay special attention to this need to rewrite the Accept method, the received parameter dir is the current path, name is a file name under the path, here is assumed to get the current path with a. jpg end of all file instances.

Heart a bit tired, such a class in good dozens of methods, the above is only part, feel more useful method.

The file record of the Java class

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.