RandomAccessFile
1、RandomAccessFile构造方法
RandomAccessFile (File file,
String mode) throws FileNotFoundException创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。 |
RandomAccessFile (String name,
String mode) throws FileNotFoundException 创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。 |
"r" | 只读模式 |
"w" | 只写模式 |
"rw" | 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件 |
2、RandomAccessFile的常用方法
部分方法的的示例代码
a)、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.*;
2 public class TestRandomAccessFile1{
3 public static void main(String[] args){
4 File file = null;
5 RandomAccessFile raf = null;
6 String name = null ;
7 int no = 0 ;
8 try{
9 file = new File("F:"+File.separator+"java"+File.separator+"IO"+File.separator+"RandomAccessFile"+File.separator+"TestRandomAccessFile.txt");
10 //读写模式,如果文件不存在,会自动创建
11 raf = new RandomAccessFile(file,"rw");
12 name = "bchxsx " ; // 字符串长度为8
13 no = 1 ; // 数字的长度为4
14 raf.writeInt(no) ; // 将学号写入文件之中
15 raf.writeBytes(name) ; // 将姓名写入文件之中
16 name = "zhx " ; // 字符串长度为8
17 no = 2 ; // 数字的长度为4
18 raf.writeInt(no) ; // 将学号写入文件之中
19 raf.writeBytes(name) ; // 将姓名写入文件之中
20 name = "zj " ; // 字符串长度为8
21 no = 3 ; // 数字的长度为4
22 raf.writeInt(no) ; // 将学号写入文件之中
23 raf.writeBytes(name) ; // 将姓名写入文件之中
24 }catch(FileNotFoundException e){
25 e.printStackTrace();
26 }catch(IOException e){
27 e.printStackTrace();
28 }finally{
29 try{
30 if(raf!=null)
31 raf.close(); // 关闭
32 }catch(IOException e){
33 e.printStackTrace();
34 }
35 }
36 }
37 }
b)、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.File ;
2 import java.io.RandomAccessFile ;
3 public class TestRandomAccessFile2{
4 // 所有的异常直接抛出,程序中不再进行处理
5 public static void main(String args[]) throws Exception{
6 File file = new File("F:"+File.separator+"java"+File.separator +"IO"+File.separator+"RandomAccessFile"+File.separator
7 +"TestRandomAccessFile.txt") ; // 指定要操作的文件
8 // 声明RandomAccessFile类的对象
9 RandomAccessFile raf = null ;
10 // 以只读的方式打开文件
11 raf = new RandomAccessFile(file,"r") ;
12 String name = null ;
13 int no = 0 ;
14 byte b[] = new byte[8] ; // 开辟byte数组
15
16 // 读取第二个人的信息,意味着要空出第一个人的信息
17 raf.skipBytes(12) ; // 跳过第一个人的信息
18 for(int i=0;i<b.length;i++){
19 b[i] = raf.readByte() ; // 读取一个字节
20 }
21 name = new String(b) ;// 将读取出来的byte数组变为字符串
22 no = raf.readInt() ; // 读取数字
23 System.out.println("第二个人的学号:" + no +"姓名:" + name ) ;
24
25
26 // 读取第一个人的信息
27 raf.seek(0) ; // 指针回到文件的开头
28 for(int i=0;i<b.length;i++){
29 b[i] = raf.readByte() ; // 读取一个字节
30 }
31 name = new String(b) ;// 将读取出来的byte数组变为字符串
32 no = raf.readInt() ; // 读取数字
33 System.out.println("第一个人的学号:" + no +"姓名:" + name ) ;
34
35
36 raf.skipBytes(12) ; // 空出第二个人的信息
37 for(int i=0;i<b.length;i++){
38 b[i] = raf.readByte() ; // 读取一个字节
39 }
40 name = new String(b) ;// 将读取出来的byte数组变为字符串
41 no = raf.readInt() ; // 读取数字
42 System.out.println("第三个人的学号:" + no +"姓名:" + name ) ;
43 raf.close() ; // 关闭
44 }
45 }
RandomAccessFile
1、RandomAccessFile构造方法
|
|
|
||
|
||
|
2、RandomAccessFile的常用方法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|