在正式介绍如何使用Java的输入/输出相关类来进行文件存取前,先简单地通过使用java.io.RandomAccessFile来存取文件,以认识一些文件存取时所必须注意的概念与事项。
文件存取通常是循序的,每在文件中存取一次,文件的读取位置就会相对于目前的位置前进一次。然而有时必须指定文件的某个区段进行读取或写入的动作,也就是进行随机存取(Random Access),即要能在文件中随意地移动读取位置。这时可以使用RandomAccessFile,使用它的seek()方法来指定文件存取的位置,指定的单位是字节。
为了移动存取位置时的方便,通常在随机存取文件中会固定每一个数据的长度。例如长度固定为每一个学生个人数据,Java中并没有直接的方法可以写入一个固定长度数据(像C/C++中的structure),所以在固定每一个长度方面必须自行设计
public class GreatStudent { private String name; private int score; public GreatStudent() { setName("noname"); } public GreatStudent(String name, int score) { setName(name); this.score = score; } public void setName(String name) { StringBuilder builder = null; if(name != null) builder = new StringBuilder(name); else builder = new StringBuilder(15); builder.setLength(15); // 最长 15 字符 this.name = builder.toString(); } public void setScore(int score) { this.score = score; } public String getName() { return name; } public int getScore() { return score; } // 每个数据固定写入34字节 public static int size() { return 34; }}
对于每一个学生数据的实例在写入文件时,会固定以34字节的长度写入,也就是15个字符(30字节)加上一个int整数的长度(4字节)。
import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.util.Scanner;public class RandomAccessFileDemo { public static void main(String[] args) { GreatStudent[] students = { new GreatStudent("Justin", 90), new GreatStudent("momor", 95), new GreatStudent("Bush", 88), new GreatStudent("caterpillar", 84)}; try { File file = new File(System.getProperty("user.dir")+File.separator+"t.txt"); // 建立RandomAccessFile实例并以读写模式打开文件 RandomAccessFile randomAccessFile =new RandomAccessFile(file, "rw"); for(int i = 0; i < students.length; i++) { // 使用对应的write方法写入数据 randomAccessFile.writeChars(students[i].getName()); randomAccessFile.writeInt(students[i].getScore()); } Scanner scanner = new Scanner(System.in); System.out.print("读取第几个数据?"); int num = scanner.nextInt(); // 使用seek()方法操作存取位置 randomAccessFile.seek((num-1) * GreatStudent.size()); GreatStudent student = new GreatStudent(); // 使用对应的read方法读出数据 student.setName(readName(randomAccessFile)); student.setScore(randomAccessFile.readInt()); System.out.println("姓名:" + student.getName()); System.out.println("分数:" + student.getScore()); // 设置关闭文件 randomAccessFile.close(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("请指定文件名称"); } catch(IOException e) { e.printStackTrace(); } } private static String readName(RandomAccessFile randomAccessfile) throws IOException { char[] name = new char[15]; for(int i = 0; i < name.length; i++) name[i] = randomAccessfile.readChar(); // 将空字符取代为空格符并返回 return new String(name).replace('\0', ' '); }}
RandomAccessFile上的相关方法实现都在批注中说明了,可以看到读写文件时几个必要的流程:
打开文件并指定读写方式 在Java中,当实例化一个与文件相关的输入/输出类时,就会进行打开文件的动作。在实例化的同时要指定文件是要以读出(r)、写入(w)或可读可写(rw)的方式打开,可以将文件看作是一个容器,要读出或写入数据都必须打开容器的瓶盖。
使用对应的写入方法 对文件进行写入,要使用对应的写入方法。在Java中通常是write的名称作为开头,在低级的文件写入中,要写入某种类型的数据,就要使用对应该类型的方法,如writeInt()、writeChar()等。
使用对应的读出方法 对文件进行读出,要使用对应的读出方法。在Java中通常是read的名称作为开头,在低级的文件读出中,要读出某种类型的数据,就要使用对应该类型的方法,如readInt()、readChar()等。
关闭文件 可以将文件看作是一个容器,要读出或写入数据都必须打开容器的瓶盖,而不进行读出或写入时,就要将瓶盖关闭。对于某些文件存取对象来说,关闭文件的动作意味着将缓冲区(Buffer)的数据全部写入文件,如果不作关闭文件的动作,某些数据可能没有写入文件而遗失。