Java代码
// 关于创建参数
new RandomAccessFile(filePath, "r"); // 只读 文件不存在时会抛出异常
new RandomAccessFile(filePath, "rw"); // 读写 文件不存在时 会创建文件
new RandomAccessFile(filePath, "rws"); // 读写 内容和元数据均同步 文件不存在时 会创建文件
new RandomAccessFile(filePath, "rwd"); // 读写 内容同步 文件不存在时 会创建文件
// 关于创建参数
new RandomAccessFile(filePath, "r"); // 只读 文件不存在时会抛出异常
new RandomAccessFile(filePath, "rw"); // 读写 文件不存在时会创建文件
new RandomAccessFile(filePath, "rws"); // 读写 内容和元数据均同步文件不存在时 会创建文件
new RandomAccessFile(filePath, "rwd"); // 读写 内容同步文件不存在时 会创建文件
解释一下rws与rwd的区别,rws会在同步内容时保持元数据的同步 例如最后修改时间,rwd则不保证元数据的同步。
用RandomAccessFile来操作字节数据是一个不错的主意。
看一个典型的例子:
Java代码
public class RandomAccessFileStudy {
private RandomAccessFile mRdmAcsFile;
public RandomAccessFileStudy() {
//文件一开始是不存在的
String fileName = "rdm_acs_file.txt";
String filePath = RandomAccessFileStudy.class.getResource("").getPath() + fileName;
System.out.println(filePath); ///home/jpeng/story/workspace/default/JavaPrj/bin/study/base/file/rdm_acs_file.txt
try {
mRdmAcsFile = new RandomAccessFile(filePath, "rws");
System.out.println("length:" + mRdmAcsFile.length());//length:0
mRdmAcsFile.write(new byte[] { '1', '2', '3', '4', '5' });//文件内容:12345
System.out.println("length:" + mRdmAcsFile.length());//length:5
mRdmAcsFile.seek(0);
mRdmAcsFile.writeByte('a');//文件内容:a2345
System.out.println("length:" + mRdmAcsFile.length());//length:5
mRdmAcsFile.seek(1);
mRdmAcsFile.writeByte('b');//文件内容:ab345
System.out.println("length:" + mRdmAcsFile.length());//length:5
mRdmAcsFile.seek(4);
mRdmAcsFile.write(new byte[]{'x','y','z'});//文件内容:ab34xyz
System.out.println("length:" + mRdmAcsFile.length());//length:7
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
RandomAccessFileStudy rdmAcsFileStudy = new RandomAccessFileStudy();
}
}
public class RandomAccessFileStudy {
private RandomAccessFile mRdmAcsFile;
public RandomAccessFileStudy() {
//文件一开始是不存在的
String fileName = "rdm_acs_file.txt";
String filePath = RandomAccessFileStudy.class.getResource("").getPath() + fileName;
System.out.println(filePath); ///home/jpeng/story/workspace/default/JavaPrj/bin/study/base/file/rdm_acs_file.txt
try {
mRdmAcsFile = new RandomAccessFile(filePath, "rws");
System.out.println("length:" + mRdmAcsFile.length());//length:0
mRdmAcsFile.write(new byte[] { '1', '2', '3', '4', '5' });//文件内容:12345
System.out.println("length:" + mRdmAcsFile.length());//length:5
mRdmAcsFile.seek(0);
mRdmAcsFile.writeByte('a');//文件内容:a2345
System.out.println("length:" + mRdmAcsFile.length());//length:5
mRdmAcsFile.seek(1);
mRdmAcsFile.writeByte('b');//文件内容:ab345
System.out.println("length:" + mRdmAcsFile.length());//length:5
mRdmAcsFile.seek(4);
mRdmAcsFile.write(new byte[]{'x','y','z'});//文件内容:ab34xyz
System.out.println("length:" + mRdmAcsFile.length());//length:7
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
RandomAccessFileStudy rdmAcsFileStudy = new RandomAccessFileStudy();
}
}
RandomAccessFile提供了setLength方法,可以让我们在不知道文件内容情况下设置文件大小,使其占用足够的磁盘空间,然后使用seek方法进行制定位置的替换写入操作,这在资源下载时非常有用。