-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert.java
More file actions
40 lines (40 loc) · 927 Bytes
/
Insert.java
File metadata and controls
40 lines (40 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//向指定文件的指定位置插入文件使用RandomAccessFile类创建文件
import java.io.*;
public class Insert
{
public static void insert(String s,String name,long index)
throws IOException
{
File tmp = File.createTempFile("tmp",null);
tmp.deleteOnExit();
try(RandomAccessFile ra = new RandomAccessFile(name,"rw");
FileInputStream fi = new FileInputStream(tmp);
FileOutputStream fo = new FileOutputStream(tmp))
{
System.out.print(ra.getFilePointer());
ra.seek(index);
byte[] b =new byte[32];
int hasRead = 0;
while ((hasRead = ra.read(b))>0)
{
fo.write(b, 0 , hasRead);
}
ra.seek(index);
ra.writeBytes(s);
hasRead = 0;
while ((hasRead = fi.read(b))>0)
{
ra.write(b, 0 , hasRead);
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
throws IOException
{
insert("*********************************","a.txt", 10);
}
}