In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.