Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java implements random reading and writing of files-RandomAccessFile

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

RandomAccessFile is the most functional file content access class in the input and output stream system of Java. It provides many methods to operate files, including read and write support. Compared with ordinary IO streams, its biggest special feature is that it supports arbitrary access, and the program can directly jump to any place to read and write data.

If we only want to access parts of the file instead of reading it from beginning to end, using RandomAccessFile will lead to cleaner code and better performance.

Let's take a look at the two more important methods in the RandomAccessFile class, and the others are similar to ordinary IO, which will not be described in detail here.

The method name functions getFilePointer () to return the current position of the file record pointer seek (long pos) to position the file record pointer to the location of pos

Function one, read data from any location, the code is as follows

Public static void randomRed (String path,int pointe) {

Try {

RandomAccessFile raf=new RandomAccessFile (path, "r")

Raf.seek (pointe); / / move the file pointer location

Byte [] buff=new byte [1024]

/ / used to save the number of bytes actually read

Int hasRead=0

/ / Loop read

While ((hasRead=raf.read (buff)) > 0) {

/ / print the contents of the read and convert bytes into string input

System.out.println (new String (buff,0,hasRead))

}

} catch (Exception e) {

E.printStackTrace ()

}

} so far, several functions of the RandomAccessFile class have been implemented in the code. Now back to the requirement mentioned at the beginning of this article, it can be easily completed with the RandomAccessFile class. In addition, it should be noted that inserting data into the specified location is a function transformed by the Sanxian itself, and RandomAccessFile does not directly support it. It needs to build a new buffer temporary space, store the data, and then write, because once the amount of data is on the level. Inserting data anywhere is memory-intensive, which is why hadoop's HDFS filesystem only supports append and does not provide modification operations.

In addition, we can use the RandomAccessFile class to implement a multithreaded breakpoint download function. Friends who have used the download tool all know that two temporary files will be created before downloading, one is an empty file of the same size as the downloaded file, and the other is the location file that records the file pointer. Each time it is paused, the last pointer will be saved, and then when the breakpoint downloads, it will continue to download from the last place. In order to achieve the breakpoint download or upload function, interested friends can achieve their own.

Efficiency of RandomAccessFile read

Comparing the efficiency of Scanner and RandomAccessFile, using Scanner to read files by line is many times more efficient and takes up a little more memory, while using RandomAccessFile to read data by line is extremely inefficient, so Scanner is recommended.

The RandomAccessFile class. Its performance lags far behind that of other commonly used development languages, which seriously affects the running efficiency of the program.

Do a basic test before improving: COPY a 12-megabyte file byte by byte (here reading and writing are involved).

Read and write time (seconds) RandomAccessFileRandomAccessFile95.848BufferedInputStream + DataInputStreamBufferedOutputStream + DataOutputStream2.935

We can see that the gap between the two is about 32 times, and RandomAccessFile is too slow. As can be seen from its source code, for every byte RandomAccessFile reads / writes, it needs to perform an Imax O operation on the disk.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report