1.1java.io.file
File is used to represent one of the files or directories in the file system
Through file you can:
1: Access to the file or directory property information (name, size, modification time, etc.)
File.getname (); Get file name
File.length (); Get file length
File.lastmodified (); Gets the last modified time for a file
File.canwrite (); writable
File.canread (); readable
File.ishidden (); Do you want to hide
2: Manipulate files or directories (create, delete)
Create a new file by using file
File File = new ("Text.txt");//Create files in current directory: Test.txt not write path default is current directory
File.exists (); Boolean exists ()//Determines whether the file or directory currently represented by
To delete a file by using file
File File = new file ("Text.txt");
File.delete ();//If you have this file to delete, you should add exists () to judge
Use file to create a directory mkdir (); You can write all the paths into the Create multilevel directory.
To delete a directory using file:
Deleting a directory using the file's Delete method requires that the directory be an empty directory
if (dir.exists ()) {
Dir.delete ();//Delete Empty directory methods
}
Use file to get all the subkeys in a directory:
Gets all subkeys in the current directory, file dir = new file (".");
Determines whether file represents a document or a directory Boolean isfile (); Boolean isdirectory ()
File[] Listfiles () Gets the current file representation of all the subkeys in the directory returned by each element in the array as a subkey in that directory.
3: Access to a directory subkey but cannot read file data.
File provides an overloaded Listfiles method that allows you to pass in a filter that only returns a subkey that satisfies that filter requirement in the directory represented by file.
FileFilter filter = new FileFilter () {//Filter condition
/**
* Define filter conditions when the parameter file is considered to satisfy
* The Accept method should return true when filtering requirements
*/
Public boolean accept (file file) {
return File.isfile ();
}
};
1.2java.io.randomaccessfile is designed to read and write files. RAF read-write files are based on file pointers, i.e. the RAF always reads and writes bytes in the file location pointed to by the file pointer. And after reading and writing, the pointer automatically moves back to the next byte position.
*randomaccessfile raf= New Randomaccessfile ("Raf.dat", "RW");
Writes out bytes to the Raf.dat file;
You need to specify the operation permissions when creating the RAF.
Permissions are commonly used for:
R: Read-only mode, read only for file data
RW: Read/write mode
If the RAF operation of the file does not exist, then the RAF is the RW mode can automatically create the file, but if the R mode, will throw the file does not exist exception.
void write (int d) writes 1 bytes to the file, and writes the lower eight bits of the 2 binary 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 bytes and returns in int form. A return value of 1 indicates that the end of the file is read
int read (byte[] data) reads the bytes of the total length of the given byte array from the file once, and deposits it into the array, the return value is the actual read to the amount of bytes, if the return value is-1, it means that this time not read to any bytes (read the end of the file)
void Write (byte[] date) writes out all the bytes in a given byte array once
void Write (byte[] data,int offset,int Len) writes out the given byte array from a continuous Len byte starting at the subscript offset
I am a beginner, if the update is not good, welcome the Great God pointed out, thank you!
More exciting after the update, reprinted annotated!
java-file files Simple and practical