Home  >  Article  >  Java  >  Java Tutorial File Simple and Practical Method

Java Tutorial File Simple and Practical Method

巴扎黑
巴扎黑Original
2017-09-08 09:42:531419browse

The following editor will bring you a simple and practical method of Java-File (share). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look

1.1java.io.File

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

Through File you can:

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

file.getName(); Get the file name

file.length(); Get the file length

file.lastModified(); Get the last modification of the file Time

file.canWrite(); Whether it can be written

file.canRead(); Whether it can be read

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 a file in the current directory: test.txt does not write a path and defaults to the current directory
file.exists(); boolean exists ()//Determine whether the file or directory represented by the current File already exists

Use File to delete a file

File file = new File("text.txt") ;
file.delete();//If there is this file to delete, you should add exists() to determine

Use File to create a directory mkdir(); you can write all paths into it to create a multi-level directory .

Use File to delete a directory:

When using File’s delete method to delete a directory, the directory must be an empty directory


if(dir.exists()){
dir.delete();//删除空的目录方法
}

Use File to get all subkeys in a directory:

Get all subkeys in the current directory File dir = new File(".");

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

File[] listFiles() gets each of the arrays returned by all sub-items in the directory represented by the current File The element is a subkey in this directory.

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

File provides an overloaded listFiles method that allows a filter to be passed in. This method will only return sub-items that meet the requirements of the filter in the directory represented by File.


FileFilter filter = new FileFilter(){//过滤条件
/**
* 定义过滤条件,当认为参数file满足
* 过滤要求时accept方法应当返回true
*/
public boolean accept(File file){
return file.isFile();
}

};

1.2java.io.RandomAccessFile is specially used to read and write files. RAF reads and writes files based on the file pointer, that is: RAF always Read and write bytes at the file location pointed to by the file pointer. And after reading and writing, the pointer will automatically move back to the next byte position.

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

Write bytes to the raf.dat file;

You need to specify operation permissions when creating RAF.

Commonly used permissions are:

r: read-only mode, only read file data

rw: read-write mode

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

void write(int d) writes 1 byte to the file, writing the lower eight bits of the binary system corresponding to the given int value.

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

int read() reads 1 word section and returned as an int. If the return value is -1, it means that the end of the file has been read.

int read(byte[] data) Reads the total length of the given byte array from the file at one time and stores it in the file. into the array, and the return value is the actual number of bytes read. If the return value is -1, it means that no bytes were read this time (read at the end of the file)

void write( byte[] date) writes all bytes in the given byte array at once

void write(byte[] data,int offset,int len) writes the given byte array from the subscript offset The consecutive len bytes starting at are written out at once

The above is the detailed content of Java Tutorial File Simple and Practical Method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn