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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool