Introduction to 1.RandomAccessFile
1.1 Why should I use the randomaccessfile?
We usually create the Stream object association file, start to read the file or write the file is from the beginning, not from the middle, if it is open multi-threaded Download a file we have learned before the filewriter or filereader, etc. can not be completed, and the current introduction of the randomaccessfile he can solve this problem, because it can specify a location to read, specify the location of a class written, usually in the development process, multi-threaded download a large file.
1.2. Introduction to Common methods
Construction Method: Randomaccessfile RAF = newrandomaccessfile (file file, String mode);
其中参数 mode 的值可选 "r":可读,"w" :可写,"rw":可读性;
Member Methods:
seek(int index);可以将指针移动到某个位置开始读写;setLength(long len);给写入文件预留空间:
Features and benefits of 2.RandomAccessFile
This object has two advantages
1. Can be read or write
Randomaccessfile does not belong to the InputStream and OutputStream classes, it is a completely independent class, all methods (the vast majority of them only belong to itself) are self-starting from the beginning of the provision, where bread contains read and write two operations
2. Can specify the location to read and write
Randomaccessfile can move around the file, move in the file with Seek (), so its behavior is fundamentally different from other I/O classes. In summary, it is a separate class that inherits directly from object. Only Randomaccessfile has a seek search method, and this method applies only to files.
3. Use cases to familiarize yourself with the most common operations of randomaccessfile
First, create a Downloadthread class to inherit the thread
public class Downloadthread extends Thread {private long start; Private File src; Private long total; Private File desc; /** * * @param start * Download location * @param src * file to download * @param desc * Destination to download * @param total * amount to download */public Downloadthread (long start, file src, file des C, long total) {This.start = start; THIS.SRC = src; THIS.DESC = desc; This.total = total; } @Override public void Run () {try {//Create Input Stream Association source, because to specify location read and write, so we need to use random access stream RANDOMACCESSF ile src = new Randomaccessfile (THIS.SRC, "RW"); Randomaccessfile desc = new Randomaccessfile (This.desc, "RW"); Source and purpose are to start from Start Src.seek (start); Desc.seek (start); Start reading and writing byte[] arr = new byte[1024]; int Len; Long Count = 0; while (len = Src.read (arr))! =-1) { There are three cases if (Len + Count > Total) {//1. When you read it, you need to change Len len = (int) (total-count); Desc.write (arr, 0, Len); Prove that the thread download task is complete, end read and write operation break; } else if (len + Count < total) {//2. proves that there is not enough download volume to directly write the content to Desc.write (arr, 0, Len); and make the counter task accumulate count + = arr.length; } else {//3. Prove to be good to download total desc.write (arr, 0, Len); End read and write break; }} src.close (); Desc.close (); } catch (Exception e) {e.printstacktrace (); } }}
Then define the main method to test the file
public class TestRandomAccess { public static void main(String[] args) { //关联源 File src = new File("a.txt"); //关联目的 File desc = new File("b.txt"); //获取源的总大小 long length = src.length(); // 开两条线程,并分配下载任务 new DownLoadThread(0, src, desc, length / 2).start(); new DownLoadThread(length / 2 , src, desc, length - (length / 2)).start(); }}
4. Effect display
A.txt's Content
B.txt's Content
5. Summary
From the above analysis can be seen randomaccessfile maximum of two features:
1. You can specify the position to start the operation;
2. Can be read, can also write;
Therefore, we can use the Randomaccessfile class when we encounter the need to start reading from the middle part of the file, for example: multi-threaded download is the most commonly used scenario
Common uses of Randomaccessfile