Java File File Simple utility method of share

  • 2020-09-16 07:28:06
  • OfStack

1.1java.io.File

File is used to represent a file or directory in a file system

Via File:

1: Access the property information of the file or directory (name, size, modification time, etc.)

file.getName(); Get file name

file.length(); Get file length

file.lastModified(); Gets the last modification time of the file

file.canWrite(); Could you write

file.canRead(); Is readable

file.isHidden(); Whether to hide

2: Manipulate files or directories (create, delete)

Use File to create a new file

File file = new File("text.txt"); // Create file in current directory: test.txt do not write path the default is the current directory
file. exists (); boolean exists()// Determines whether the file or directory currently represented by File already exists

Use File to delete 1 file

File file = new File("text.txt");
file.delete(); // If this file is deleted, exists() should be added

Use File to create 1 directory mkdir(); You can write the entire path to create a multilevel directory.

Use File to delete 1 directory:

Deleting a directory using File's delete method requires that the directory be an empty directory


if(dir.exists()){
dir.delete();// Delete the empty directory method 
}

Use File to get all subitems in a directory:

Gets all subitems in the current directory File dir = new File(".");

Determines whether File represents a file or a directory boolean isFile(); boolean isDirectory()

File[] listFiles() gets all the children in the directory currently represented by File. Each element in the array returned is 1 child in that directory.

3: Access a subentry of a directory but cannot read file data.

File provides an overloaded listFiles method, allowing one filter to be passed in, which returns only the subitems in the directory represented by File that meet the filter's requirements.


FileFilter filter = new FileFilter(){// Filter conditions 
/**
*  Defines a filter condition when considered a parameter file meet 
*  Filtering requirement accept Method should return true
*/
public boolean accept(File file){
return file.isFile();
}

};

1.2 java. io. RandomAccessFile designed to read and write files RAF, speaking, reading and writing is based on the file pointer, namely: RAF is always in the file pointer to the file location for reading and writing bytes. And the pointer will automatically move back to the next byte position after read and write.

*RandomAccessFile raf= new RandomAccessFile("raf.dat","rw");

Write the bytes to the ES127en.dat file;

You need to specify action permissions when you create RAF.

Common permissions include:

r: Read-only mode, which reads only file data

rw: Read-write mode

If the file operated by RAF does not exist, then RAF will automatically create the file if it is in rw mode, but if it is in r mode, an exception will be thrown if the file does not exist.

void write(int d) writes 1 byte to the file, the lower 8 bits of base 2 corresponding to the given int value.

*RandomAccessFile raf= new RandomAccessFile("raf.dat","r"); Read 1 byte from the ES162en.dat file

int read() reads 1 byte and returns as int. A return value of -1 indicates that the end of the file was read

int read(byte[] data) reads the total length of a given byte array from a file once and stores it into the array. The return value is the actual number of bytes read. If the return value is -1, then no bytes are read this time (read at 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 a sequence of len bytes starting at the subscript offset for a given byte array

I am a beginner, if have update bad, welcome this big god point out, thank everybody!


Related articles: