RandomAccessFile基本用法

IO中有许多类可以帮我们写入数据到文件中,FileWriter,FileOutputStream以及各种被装饰的类。其中RandomAccessFile可以帮我们实现在指定位置写入内容。

从字面意思来看,通过这个类我们可以对文件进行随机访问。

RandomAccessFile对象包含了一个指针,当新建了一个RandomAccessFile对象,这个指针指向文件的开始处,即0字节的位置,当读/写了n个字节,这个指针后移到n,除此之外,指针可以根据需要自由移动到指定位置。

RandomAccessFile有两个构造器,通过传入一个File对象参数构造,或者通过String指定文件名,另外还需要指定一个参数mode,指定RandomFileAccess的访问模式,"r"为只读,"rw"为读写。

通过下面的代码,可以了解RandomAccessFile类的基本用法。

另外需要注意的一点是:

RandomAccessFile的write(byte[] arr)方法默认参数数组的编码格式为"ISO-8859-1",而中文编码格式默认为GBK,String的getBytes()方法默认编码格式为"ISO-8859-1”,因此在写入中文时,需要这一行

String.getBytes("GBK"),对编码格式进行统一!

 1 /**
 2  * RandomAccessFile:可以访问文件任意位置的类
 3  */
 4 public class RandomAccessFileTest {
 5     public static void main(String[] args) {
 6         insertAtPos(new File("F:/java.txt"),8,"沧海月明珠有泪\r\n蓝田日暖玉生烟\r\n");
 7 //        appendContent(new File("F:/java.txt"));
 8     }
 9 
10 
11     //使用范例
12     public static void test1() {
13         try (
14                 RandomAccessFile raf =
15                         new RandomAccessFile("F:/Code for SE/codeByVi/src/com/vi/io/RandomAccessFileTest.java", "r");
16         ) {
17             //获取当前指针位置
18             long index = raf.getFilePointer();
19             System.out.println("当前指针的位置:" + index);
20             //设置指针的位置
21             raf.seek(300);
22             System.out.println("现在指针的位置:" + raf.getFilePointer());
23             int hasRead = 0;
24             byte[] buffer = new byte[1024];
25             while ((hasRead = raf.read(buffer)) != -1) {
26                 System.out.print(new String(buffer, 0, hasRead));
27             }
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31     }
32 
33     /**
34      * 在文件末尾追加内容
35      */
36     public static void appendContent(File file) {
37         try (
38                 RandomAccessFile raf = new RandomAccessFile(file, "rw");
39         ) {
40             raf.seek(raf.length());
41             String content = new String("此情可待成追忆".getBytes("GBK"),"ISO8859-1");
42             raf.write("三人行必有我师".getBytes("GBK"));
43             System.out.println("追加成功!");
44         } catch (Exception e) {
45             e.printStackTrace();
46         }
47     }
48 
49     /**
50      * 将指定字符串插入文件的指定位置
51      * @param file
52      * @param pos
53      * @param content
54      */
55     public static void insertAtPos(File file,int pos,String content) {
56         try (
57                 RandomAccessFile raf = new RandomAccessFile(file,"rw");
58                 FileInputStream fr = new FileInputStream(new File("F:/temp.txt"));
59                 FileOutputStream bw = new FileOutputStream(new File("F:/temp.txt"));
60         ) {
61             raf.seek(pos);
62             byte[] buffer = new byte[64];
63             int hasRead = 0;
64             //将指定位置后的内容写入到临时文件中去
65             while((hasRead = raf.read(buffer)) != -1) {
66                 bw.write(buffer,0,hasRead);
67             }
68             //重新定位
69             raf.seek(pos);
70             //将目标内容写入到目标文件中
71             raf.write("沧海月明珠有泪".getBytes("GBK"));
72             //将临时文件中的内容写入到目标文件的末尾
73             while ((hasRead = fr.read(buffer)) != -1) {
74                 raf.write(buffer,0,hasRead);
75             }
76             new File("F:/temp.txt").delete();
77             System.out.println("写入完成!");
78         } catch (Exception e) {
79             e.printStackTrace();
80         }
81     }
82 }

猜你喜欢

转载自www.cnblogs.com/blogforvi/p/11780054.html