Class RandomAccessFile 介绍
- 该类的实例支持读取和写入随机访问文件。 随机访问文件的行为类似于存储在文件系统中的大量字节。 有一种游标,或索引到隐含的数组,称为文件指针 ; 输入操作读取从文件指针开始的字节,并使文件指针超过读取的字节。 如果在读/写模式下创建随机访问文件,则输出操作也可用; 输出操作从文件指针开始写入字节,并将文件指针提前到写入的字节。 写入隐式数组的当前端的输出操作会导致扩展数组。 文件指针可以通过读取
getFilePointer
方法和由设置seek
方法。
- 在这个类中的所有读取例程通常都是如果在读取所需的字节数之前到达文件结尾,则抛出一个
EOFException
(这是一种IOException
)。 如果任何字节由于除文件末尾之外的任何原因而无法读取,则抛出IOException
以外的EOFException
。 特别地,如果流已经被关闭,则可以抛出IOException
。
方法摘要:
String | readUTF()
从该文件读取字符串。
|
void | seek(long pos)
设置文件指针偏移,从该文件的开头测量,发生下一次读取或写入。
|
void | setLength(long newLength)
设置此文件的长度。
|
int | skipBytes(int n)
尝试跳过
n 字节的输入丢弃跳过的字节。
|
void | write(byte[] b)
从指定的字节数组写入
b.length 个字节到该文件,从当前文件指针开始。
|
void | write(byte[] b, int off, int len)
从指定的字节数组写入
len 个字节,从偏移量
off 开始写入此文件。
|
代码示例:
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 * @作者 five-five
11 * @创建时间 2020/10/15
12 */
13 public class DownloadUtil {
14 private File fromFile;//源文件
15 private File toFile;//要拷贝到的地方
16 private int threadNum;//线程数量
17
18 //下载的方法
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("线程"+(i+1));
24 System.out.println(thread.getName()+"的任务量是\t"+thread.getBlockSize());
25 thread.start();
26 }
27 System.out.println("文件上传完毕");
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;//表示第几个线程
42 private long blockSize = fromFile.length() % threadNum == 0
43 ? fromFile.length() / threadNum
44 : fromFile.length() / threadNum + (fromFile.length() % threadNum); //每个线程下载的文件大小
45 private volatile long startPos;//起始位置
46
47 /**
48 * 第多少个线程
49 *
50 * @param index
51 */
52 public MydownLoadThread(int index) {
53 this.index = index;
54 startPos = index * blockSize;//计算下载了多少
55 }
56
57 @Override
58 public void run() {
59 RandomAccessFile rf = null;
60 RandomAccessFile wf = null;
61 try {
62 // System.out.println("起始位置"+startPos);
63 //"r", "rw", "rws", or "rwd"
64 rf = new RandomAccessFile(fromFile, "r");//只读
65 wf = new RandomAccessFile(toFile, "rw");//只写
66 rf.seek(startPos);
67 wf.seek(startPos);
68 byte[] buf = new byte[1024 * 1024];//缓存区为1MB
69 int len = -1;
70 do {
71 //记录每次读取的大小(缓冲区可能大于线程任务量)
72 len = blockSize > buf.length ? buf.length : (int) blockSize;
73 //io操作
74 rf.read(buf, 0, len);
75 wf.write(buf, 0, len);
76 blockSize -= len;
77 System.out.println(Thread.currentThread().getName() + "读取了" + len + "个字节");
78 } while (blockSize>0);
79 wf.close();
80 rf.close();
81 // TimeUnit.SECONDS.sleep(1);//每次睡一秒
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 }
DownloadUtil
1 package com.edu.testThreadDlFile;
2
3 import java.io.File;
4
5 /**
6 *
7 * @作者 five-five
8 * @创建时间 2020/10/15
9 */
10 public class TestRandomAccessFile {
11 public static void main(String[] args) throws Exception {
12 File from=new File("D:\\图片\\Saved Pictures\\图片\\街道.jpg");
13 File to=new File("C:\\Users\\15713\\Desktop\\新建文件夹\\"+from.getName());
14 DownloadUtil downloadUtil = new DownloadUtil(from,to,8);
15 downloadUtil.doDownLoad();
16 }
17 }
测试代码
控制台截图:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)