java.io.RandomAccessFile
* RAF是专门用来读写文件数据的API,其基于指针对文件随机读写
* RAF没有无参构造,需要传两个参数:
第一个参数为:要操作的文件(File对象或直接给路径均可)
第二个参数为:对文件的操作模式:
- r:只读模式,仅对文件进行读取操作
- rw:读写模式,对文件数据又能读又能写
- 当操作文件的时候,在当前目录下如果没有这个文件,那么如果是rw权限,系统会自动生成,如果是r权限,会报 Exception in thread "main" java.io.FileNotFoundException 错
* 注意:随机读写不是指随机数,是指比较灵活,想在哪读就在哪读,想在哪写就在哪写
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAFdemo1 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("./raf.dat","rw");
/*
向文件中写入一个字节,写入的是指定的int值所对应的2进制度的"低八位"
vvvvvvvv
00000000 00000000 00000000 00000000
*/
raf.write(1);//00000001
raf.write(2);
raf.write(128);
System.out.println("写出完毕!");
raf.close();
}
}
从文件中读取数据-代码演示
从文件中读取1个字节,并以int形式返回。如果返回的int值为-1则表示读取到了文件末尾:EOF:end of file
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAFDemo2 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("./raf.dat","r");
/*
int read()
从文件中读取1个字节,并以int形式返回。如果返回的int值为-1则表示读取到了文件末尾
EOF:end of file
raf.dat文件内容:
00000001 00000010 10000000
*/
int d = raf.read();//00000000 00000000 00000000 00000001
System.out.println(d);//1
d = raf.read();//00000000 00000000 00000000 00000010
System.out.println(d);//2
d = raf.read();//00000000 00000000 00000000 10000000
System.out.println(d);//128
d = raf.read();//11111111 11111111 11111111 11111111
System.out.println(d);//-1 文件末尾!
raf.close();
}
}
文件的复制-单字节读写---随机读写-代码演示(效率较慢)
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class CopyDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile src=new RandomAccessFile("./kaixin.jpg","r");
RandomAccessFile desc=new RandomAccessFile("./kaixin_copy.jpg","rw");
int d;
long start=System.currentTimeMillis();
while ((d=src.read())!=-1){
desc.write(d);
}
long end=System.currentTimeMillis();
System.out.println("复制完毕!"+(end-start)+"ms");
desc.close();
src.close();
}
}
文件的复制-一组字节读写--块速读写-代码演示(效率较快)
* 通过提高每次读写的数据量,减少实际读写的次数从而可以提高读写率
* 单字节读写是随机读写形式
* 一组字节的读写是快读写形式
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
public class CopyDemo2 {
public static void main(String[] args) throws IOException {
RandomAccessFile src=new RandomAccessFile("./kaixin.jpg","r");
RandomAccessFile desc=new RandomAccessFile("./kaixin_copy.jpg","rw");
/*
int read(byte[]) data)
一次性从文件中读取给定的字节数组总长度的字节量并存入到该数组中,返回值为实际读取到的字节量
void write(byte[] data)
一次性将给定的字节数组中的所有字节写入到文件中
void write(byte[] data.int offset,int len)
一次性将给定的字节数组中从下标offest处开始的连续len个字节写入文件
*/
int len;
/*
1024byte 1kb
1024kb 1mb
1024mb 1gb
*/
byte[] data=new byte[1024*10];//10kb
long start=System.currentTimeMillis();
while ((len=src.read())!=-1){
desc.write(data,0,len);
}
long end=System.currentTimeMillis();
System.out.println("复制完毕!"+(end-start)+"ms");
desc.close();
src.close();
}
}
向文件中写入文本数据
String提供了将当前字符串转换为一组字节的方法:
- byte[ ] getBytes(String csn)
- 参数为指定的字符集的名字,常用的:utf-8
- 当字符集名字指定错误时,会抛出入异常:不支持的字符集一样
- UnsupportedEncodingException: uf-8
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
public class WriteStringDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.txt","rw");
String str="让我在看你你一遍,从南到北";
/*
String提供了将当前字符串转换为一组字节的方法:
byte[] getBytes(String csn)
参数为指定的字符集的名字,常用的:utf-8
当字符集名字指定错误时,会抛出入异常:不支持的字符集一样
UnsupportedEncodingException: uf-8
*/
byte[] data= str.getBytes("utf-8");
raf.write(data);
raf.close();
System.out.println("写出完毕");
}
}
完成简易记事本工具
程序启动后要求用户输入一个文件名字,然后对该文件进行操作, 之后用户在控制台输入的每一行内容都要写到文件中,当用户输入"exit"时程序退出
注:写入文件的数据不需要考虑换行的问题
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class Note {
public static void main(String[] args) throws IOException {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入文件名字");
String file_name=scanner.nextLine();
//创建文件
RandomAccessFile str=new RandomAccessFile(file_name,"rw");
System.out.println("请开始输入内容,单独输入exit退出");
//开始循环
while (true){
String file_data=scanner.nextLine();
if("exit".equals(file_data)){
break;
}
byte[] data=file_data.getBytes("utf-8");
str.write(data);
}
System.out.println("再见");
str.close();
}
}
从文件中读取数据
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadStringDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("沁园春","r");
byte[] data=new byte[(int)raf.length()];
raf.read(data);
String str=new String(data);
System.out.println(str);
raf.close();
}
}
RAF读写基本类型数据,以及RAF基于指针的读写操作
创建RAF后指针默认在文件的最开始处。位置的表示与数组下标一样从0开始
移动指针到指定位置(下标): raf.seek(0)
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;
public class RAFDemo3 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
long pos = raf.getFilePointer();
System.out.println("pos:"+pos);
//向文件中写入int最大值
/*
int最大值
01111111 11111111 11111111 11111111
vvvvvvvv
01111111 11111111 11111111 11111111
max>>>24
00000000 00000000 00000000 01111111
*/
int max = Integer.MAX_VALUE;
raf.write(max>>>24);
System.out.println("pos:"+raf.getFilePointer());
raf.write(max>>>16);
raf.write(max>>>8);
raf.write(max);
System.out.println("pos:"+raf.getFilePointer());
/*
RAF提供了方便写出基本类型的相关方法
*/
raf.writeInt(max);//这一句等同上面4的四句写出操作
System.out.println("pos:"+raf.getFilePointer());
raf.writeLong(12345678);
System.out.println("pos:"+raf.getFilePointer());
raf.writeDouble(123.123);
System.out.println("pos:"+raf.getFilePointer());
System.out.println("写出完毕!");
/*
raf.dat
int int long
01111111 11111111 11111111 11111111 01111111 11111111 11111111 11111111 00000000 .....
*/
/*
RAF同样提供了方便读取基本类型的方法
int readInt()
连续读取4个字节,并将其转换为对应的int值后返回。
*/
int d = raf.readInt();
System.out.println(d);
raf.seek(8);
long l = raf.readLong();
System.out.println(l);
double dou = raf.readDouble();
System.out.println(dou);
/*
readInt,readLong这些方法在读取相应的字节数的过程中如果
读取到了文件末尾则会直接抛出EOFException。并不是再用-1
表示读取到了文件末尾。
*/
// d = raf.readInt();
// System.out.println(d);
raf.close();
}
}
将user.dat文件中所有记录读取出来并输出到控制台
package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ShowAllUserDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("user.dat","r");
for(int i=0;i<raf.length()/100;i++){
//读取用户名
byte[] data=new byte[32];
raf.read(data);
String username=new String(data,"utf-8").trim();
//读取密码
raf.read(data);
String password=new String(data,"utf-8").trim();
//读取昵称
raf.read(data);
String nickname=new String(data,"utf-8").trim();
//读取年龄
int age=raf.readInt();
System.out.println(raf.getFilePointer());
System.out.println(nickname+username+password+age);
}
raf.close();
}
}