Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of Java RandomAccessFile operation

Shulou Source: shulou.com Published: 2022-06-02 04:38:24 10月02日 Update

This article mainly explains "what is the method of Java RandomAccessFile operation". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of Java RandomAccessFile operation"?

A brief introduction

RandomAccessFile:java provides access to the contents of a file, either reading or writing to it.

RandomAccessFile supports random access to files and can access any location of the file

1. Java file model

The files on the hard disk are stored in byte byte byte and are a collection of data.

2. Open the file

There are two modes "rw" (read-write) "r" (read-only)

RandomAccessFile raf = new RandomAccessFile (file, "rw")

File pointer: when opening a file, the pointer is at the beginning of pointer = 0

3. Write documents

Raf.write (int)-> writes only one byte (the last 8 bits), while the pointer points to the next location, ready to write again

4. Reading method

Int b = raf.read ()-> read a byte

5. Be sure to close the file after reading and writing

Two code implementation

Package com.imooc.io;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.util.Arrays;public class RafDemo {

/ * * @ param args * / public static void main (String [] args) throws IOException {

File demo = new File ("demo")

If (! demo.exists () demo.mkdir ()

File file = new File (demo, "raf.dat")

If (! file.exists () file.createNewFile ()

RandomAccessFile raf = new RandomAccessFile (file, "rw")

/ / position of the pointer

System.out.println (raf.getFilePointer ())

Raf.write ('A')

/ / only one byte was written

System.out.println (raf.getFilePointer ())

Raf.write ('B')

Int I = 0x7fffffff

/ / you can only write one byte at a time with the write method. If you want to write I, you have to write raf.write (I > 24) 4 times; / / High 8-bit raf.write (I > 16); raf.write (I > > 8); raf.write (I)

System.out.println (raf.getFilePointer ())

/ / you can write an int raf.writeInt (I) directly

String s = "medium"

Byte [] gbk = s.getBytes ("gbk")

Raf.write (gbk)

System.out.println (raf.length ())

/ / to read the file, you must move the pointer to the header

Raf.seek (0)

/ / read all the contents of the file into the byte array at one time

Byte [] buf = new byte [(int) raf.length ()]

Raf.read (buf)

System.out.println (Arrays.toString (buf))

For (byte b: buf) {

System.out.println (Integer.toHexString (b & 0xff) + "")

}

Raf.close ()

}}

Third, realize the effect

01612 [65, 66, 127,-1,-1,-1,127,-1,-1,-1,-42,-48] 41427fffffff7fffffffd6d0

At this point, I believe you have a deeper understanding of "what is the method of Java RandomAccessFile operation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File method byte pointer content location learning practical deeper one-time code interest again at the same time practical beginning pointing simple operation effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia macOS MariaDB Shulou Tech Info Microsoft