Java-File is simple and practical, and java-file is simple and practical.

Source: Internet
Author: User

Java-File is simple and practical, and java-file is simple and practical.

1.1java.io.File
File indicates a File or directory in the File system.
You can use File:
1: access the attribute information (name, size, modification time, etc.) of the file or directory)
File. getName (); get the file name
File. length (); get the object length
File. lastModified (); get the last file modification time
File. canWrite (); writable?
File. canRead (); readable
File. isHidden (); Whether to hide
2: operate on files or directories (create and delete)
Use File to create a new File
File file = new File ("text.txt"); // create a File in the current directory: test.txt the default path is the current directory.
File. exists (); boolean exists () // checks whether the File or directory in the current file already exists.
Use File to delete an object
File file = new File ("text.txt ");
File. delete (); // if this file is deleted, add exists () to judge
Use File to create a directory mkdir (); you can write all the paths into the create multi-level directory.
Use File to delete a directory:
When you delete a directory using the delete method of File, the directory must be an empty directory.
If (dir. exists ()){
Dir. delete (); // delete an empty directory
}
Use File to obtain all the sub-items in a directory:
Get all the sub-items in the current directory File dir = new File (".");
Determine whether the File represents a File or a directory boolean isFile (); boolean isDirectory ()
File [] listFiles () gets each element in the array returned by all sub-items in the directory represented by the current File as one sub-item in the directory.
3: Access the sub-item of a directory but cannot read the file data.

File provides an overloaded listFiles method that allows you to pass in a filter. This method only returns the Sub-item that meets the filter requirements in the directory indicated by File.
FileFilter filter = new FileFilter () {// filter Condition
/**
* Defines filter conditions. When the file parameter is considered to meet
* When filtering requirements, the accept method should return true
*/
Public boolean accept (File file ){
Return file. isFile ();
}

};

1.2java.io.RandomAccessFile is used to read and write files in RAF based on the file pointer. That is, RAF always reads and writes bytes at the file location pointed to by the file pointer. After reading and writing, the pointer automatically moves to the next byte location.
* RandomAccessFile raf = new RandomAccessFile ("raf. dat", "rw ");
Write bytes to the raf. dat file;
You must specify the operation permission when creating the RAF.
Common permissions include:
R: Read-only mode. Only file data is read.
Rw: read/write mode
If the file operated by RAF does not exist, the file can be created automatically when RAF is in rw mode. However, if it is in r mode, an exception that does not exist in the file will be thrown.
Void write (int d) writes one byte to the file, which is the low eight bits of the binary system corresponding to the given int value.
* RandomAccessFile raf = new RandomAccessFile ("raf. dat", "r"); read a byte from the raf. dat file
Int read () reads 1 byte and returns it in the form of int. If the returned value is-1, it indicates that the object is read to the end.

Int read (byte [] data) reads the total length of the given byte array from the file at a time, and then enters the array. The returned value is the actual number of bytes read, if the returned value is-1, it indicates that no Bytes are read this time (read at the end of the file)
Void write (byte [] date) writes all the bytes in the given byte array at a time.
Void write (byte [] data, int offset, int len) writes the given byte array of consecutive len bytes starting from the subscript offset

 

I am a beginner. If any updates are poor, I would like to thank you!

More highlights will be updated later!

 

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.