Class RandomAccessFile
- Instances of this class support reading and writing random access files. Random access to a file behaves like a large number of bytes stored in a file system. There is a cursor, or index to an implicit array, called a file pointer; The input operation reads the bytes starting from the file pointer and causes the file pointer to exceed the read bytes. If a random access file is created in read / write mode, the output operation is also available; The output operation starts writing bytes from the file pointer and advances the file pointer to the written bytes. An output operation that writes to the current end of an implicit array causes the array to be extended. The file pointer can be set by reading the getFilePointer method and by setting the seek method.
- All reading routines in this class usually throw an EOFException (an IOException) if they reach the end of the file before reading the required number of bytes. If any byte cannot be read for any reason other than the end of the file, an EOFException other than IOException is thrown. In particular, IOException can be thrown if the flow has been closed.
Method summary:
()
Read the string from this file.
|
|
void | (long pos)
Set the file pointer offset, measure from the beginning of the file, and the next read or write occurs.
|
void | (long newLength)
Set the length of this file.
|
int | (int n)
Try skipping n bytes of input and discard the skipped bytes.
|
void | (byte[] b)
Write b.length bytes from the specified byte array to the file, starting from the current file pointer.
|
void | (byte[] b, int off, int len)
Write len bytes from the specified byte array and write this file from offset off.
|
Code example:
1 package com.edu.testThreadDlFile; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.io.RandomAccessFile; 7 import java.util.concurrent.TimeUnit; 8 9 /** 10 * @Author five five 11 * @Created on October 15, 2020 12 */ 13 public class DownloadUtil { 14 private File fromFile;//source file 15 private File toFile;//Where to copy 16 private int threadNum;//Number of threads 17 18 //Download method 19 public void doDownLoad() { 20 System.out.println(fromFile.length()); 21 for (int i=0;i<threadNum;i++){ 22 MydownLoadThread thread = new MydownLoadThread(i); 23 thread.setName("thread "+(i+1)); 24 System.out.println(thread.getName()+"The number of tasks is\t"+thread.getBlockSize()); 25 thread.start(); 26 } 27 System.out.println("File upload completed"); 28 } 29 30 public DownloadUtil() { 31 } 32 33 public DownloadUtil(File fromFile, File toFile, int threadNum) { 34 this.fromFile = fromFile; 35 this.toFile = toFile; 36 this.threadNum = threadNum; 37 } 38 39 class MydownLoadThread extends Thread { 40 41 private volatile int index;//Indicates the number of threads 42 private long blockSize = fromFile.length() % threadNum == 0 43 ? fromFile.length() / threadNum 44 : fromFile.length() / threadNum + (fromFile.length() % threadNum); //File size downloaded per thread 45 private volatile long startPos;//Starting position 46 47 /** 48 * How many threads 49 * 50 * @param index 51 */ 52 public MydownLoadThread(int index) { 53 this.index = index; 54 startPos = index * blockSize;//Calculate how many downloads 55 } 56 57 @Override 58 public void run() { 59 RandomAccessFile rf = null; 60 RandomAccessFile wf = null; 61 try { 62 // System.out.println("Starting position"+startPos); 63 //"r", "rw", "rws", or "rwd" 64 rf = new RandomAccessFile(fromFile, "r");//read-only 65 wf = new RandomAccessFile(toFile, "rw");//Write only 66 rf.seek(startPos); 67 wf.seek(startPos); 68 byte[] buf = new byte[1024 * 1024];//Cache is 1 MB 69 int len = -1; 70 do { 71 //Record the size of each read(The buffer may be larger than the number of thread tasks) 72 len = blockSize > buf.length ? buf.length : (int) blockSize; 73 //io operation 74 rf.read(buf, 0, len); 75 wf.write(buf, 0, len); 76 blockSize -= len; 77 System.out.println(Thread.currentThread().getName() + "Read" + len + "Bytes"); 78 } while (blockSize>0); 79 wf.close(); 80 rf.close(); 81 // TimeUnit.SECONDS.sleep(1);//Sleep one second at a time 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 87 public int getIndex() { 88 return index; 89 } 90 91 public void setIndex(int index) { 92 this.index = index; 93 } 94 95 public long getBlockSize() { 96 return blockSize; 97 } 98 99 public void setBlockSize(long blockSize) { 100 this.blockSize = blockSize; 101 } 102 103 public long getStartPos() { 104 return startPos; 105 } 106 107 public void setStartPos(long startPos) { 108 this.startPos = startPos; 109 } 110 } 111 112 public File getFromFile() { 113 return fromFile; 114 } 115 116 public void setFromFile(File fromFile) { 117 this.fromFile = fromFile; 118 } 119 120 public File getToFile() { 121 return toFile; 122 } 123 124 public void setToFile(File toFile) { 125 this.toFile = toFile; 126 } 127 128 public int getThreadNum() { 129 return threadNum; 130 } 131 132 public void setThreadNum(int threadNum) { 133 this.threadNum = threadNum; 134 } 135 }
1 package com.edu.testThreadDlFile; 2 3 import java.io.File; 4 5 /** 6 * 7 * @Author five five 8 * @Created on October 15, 2020 9 */ 10 public class TestRandomAccessFile { 11 public static void main(String[] args) throws Exception { 12 File from=new File("D:\\picture\\Saved Pictures\\picture\\street.jpg"); 13 File to=new File("C:\\Users\\15713\\Desktop\\New folder\\"+from.getName()); 14 DownloadUtil downloadUtil = new DownloadUtil(from,to,8); 15 downloadUtil.doDownLoad(); 16 } 17 }
Screenshot of console: