1.使用RandomAccessFile写文件数据(一字节)
file.write(int d);——>int低八位写出
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* java.io.RandomAccessFile
* 用于读写文件数据的类
* RAF读写文件数据总是在指针当前位置进行读或写,
* 并且读写后指针会自动后移。
* 指针是指向文件数据位置的标记(底层实现)。
* @author soft01
*
*/
public class RandomAccessFile_write {
public static void main(String[] args) throws IOException {
/*
* 第二个参数是读写模式,常用的有:
* "r":只读模式,该模式要求读取的文件必须存在
* "rw":读写模式,该模式若文件不存在会自动创建
* read write
*/
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
/*
* void write(int d)
* 向文件中写入1个字节,写的是给定int值对应2进制的“低八位”
* vvvvvvvv
* 00000000 00000000 00000000 00000001
*/
raf.write(1);;
System.out.println("写出完毕");
//读写完毕最终要close
raf.close();
}
}
2.使用RandomAccessFile读文件数据(一字节)
file.read()——>读取一字节
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 读取一个字节
* @author soft01
*
*/
public class RandomAccessFile_read {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
/*
* 读取一个字节,并以int形式返回
* 若返回值为-1表示读取到了文件末尾
*/
int d = raf.read();
System.out.println(d);
//由于文件只有一次字节,再次读取会返回-1
d = raf.read();
System.out.println(d);//-1
raf.close();
}
}
3.使用RandomAccessFile复制文件数据(一字节复制)
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
public class CopyDemo {
public static void main(String[] args) throws IOException {
RandomAccessFile src = new RandomAccessFile("0.png","r");
RandomAccessFile desc = new RandomAccessFile("1.png","rw");
long start = System.currentTimeMillis();
int d=-1;
while((d=src.read())!=-1) {
desc.write(d);
}
long end = System.currentTimeMillis();
System.out.println("复制完毕!耗时"+(end-start));
src.close();
desc.close();
}
}
4.使用RandomAccessFile复制文件数据(多字节复制)
write(byte[] data,int offset,int len)——>多字节复制
read(byte[] data)——>将数组中的所有字节读出
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 提高每次读写的数据
* @author soft01
*
*/
public class CopyDemo2 {
public static void main(String[] args) throws IOException {
RandomAccessFile src = new RandomAccessFile("0.png","r");
RandomAccessFile desc = new RandomAccessFile("1.png","rw");
/*
* RAF提供了批量读写字节的方法:
* int read(byte[] data)
* 一次性读取给定字节数组总长度的字节量并将读到的字节存入到该数组中。
* 返回值为实际读取到的字节量,若返回值为-1表示本次没有读取到任何字节(文件末尾读取)
*/
byte[] data = new byte[1024*10];//10k
int len = -1;//每次实际读取到的字节数
long start = System.currentTimeMillis();
while((len=src.read(data))!=-1) {
/*
* write(byte[] data)
* 一次性将给定字节数组中的所有字节写出
*
* write(byte[] data,int offset,int len)
* 将给定字节数组从下标为offset处的连续len一次性写出
*/
desc.write(data,0,len);
}
long end = System.currentTimeMillis();
System.out.println("复制完毕!耗时:"+(end-start));
}
}
5.使用RandomAccessFile在文件里写字符串
write(byte[] data)——>写字符串
package day04;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 使用RandomAccessFile写字符串
* @author soft01
*
*/
public class RAF_write {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
String str = "hello";
byte[] data = str.getBytes("UTF-8");//里面可以不填,也可以填编码方式
raf.write(data);
System.out.println("写出完毕");
raf.close();
}
}
6.使用RandomAccessFile读取文件内容
package day04_;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAF_read {
public static void main(String[] args) throws IOException {
/*
* 将raf.txt文件中的内容读取出来
*/
RandomAccessFile raf = new RandomAccessFile("raf.txt","r");
byte[] data = new byte[100];
int len = raf.read(data);
/**
* String(byte[] data)
* 将给定的字节数组中所有字节按照系统默认的字符集转换为对应的字符串
*
* String(byte[] data,int offset,int len)
* 将给定的字节数组中从下标为offset处开始的连续len个字节
* 按照按照系统默认的字符集转换为对应的字符串
*/
String str = new String(data,0,len,"utf-8");
System.out.println(str);
raf.close();
}
}
7.使用RandomAccessFile读写基本类型数据以及对于指针的操作
file.getFilePointer()——>获取当前RandomAccessFile的指针位置
file.seek(0);——>移动RandomAccessFile的指针位置
package day04_;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* RAF读写基本类型数据以及RAF对于指针的操作
* @author soft01
*
*/
public class RAF_wr_type {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
/*
* long getFilePointer()
* 获取文件指针位置
*/
long pos = raf.getFilePointer();
System.out.println("pos:"+pos);//0
int imax = Integer.MAX_VALUE;
/*
* int最大值的2进制形式:
* vvvvvvvv
* 01111111 11111111 11111111 11111111
*/
raf.write(imax>>>24);
System.out.println("pos:"+raf.getFilePointer());
raf.write(imax>>>16);
System.out.println("pos:"+raf.getFilePointer());
raf.write(imax>>>8);
raf.write(imax);
/*
* 一次性写出4个字节,将给定的int值写出
*/
raf.writeInt(imax);
System.out.println("pos:"+raf.getFilePointer());
raf.writeLong(123l);
System.out.println("pos:"+raf.getFilePointer());
raf.writeDouble(123.123);
System.out.println("pos:"+raf.getFilePointer());
//操作指针
raf.seek(0);
System.out.println("pos:"+raf.getFilePointer());
int d = raf.readInt();
System.out.println(d);
System.out.println("pos:"+raf.getFilePointer());
//读取long
//1:移动指针到long值的第一个字节的所在位置
raf.seek(8);
//2:连续读取8个字节还原为该long值
long l = raf.readLong();
System.out.println(l);
System.out.println("pos:"+raf.getFilePointer());
double dou = raf.readDouble();
System.out.println(dou);
System.out.println("pos:"+raf.getFilePointer());
raf.close();
}
}
8.使用RandomAccessFile解析emp.dat文件,将10个员工信息输出
package day04_;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 完成简易记事本程序,使用文件流
* 解析emp.dat文件,将10个员工信息输出
* @author soft01
*
*/
public class Test {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("emp.dat","r");
for(int i=0;i<10;i++) {
//读取员工姓名 32字节的字符串
String name = readString(raf,32);
//读取年龄
int age = raf.readInt();
//读取性别
String gender = readString(raf,10);
//读取工资
int salary = raf.readInt();
//读取入职日期
String hiredate = readString(raf,30);
System.out.println(name+","+age+","+gender+","+","+salary+","+hiredate);
}
/*for(int i=0;i<10;i++) {
//读取员工姓名 32字节的字符串
byte[] data = new byte[32];
raf.read(data);
String name = new String(data).trim();
System.out.println("name:"+name);
System.out.println("pos:"+raf.getFilePointer());
//读取年龄
int age = raf.readInt();
System.out.println("age:"+age);
System.out.println("pos:"+raf.getFilePointer());
//读取性别
data = new byte[10];
raf.read(data);
String gender = new String(data).trim();
System.out.println("gender:"+gender);
System.out.println("pos:"+raf.getFilePointer());
//读取工资
int salary = raf.readInt();
System.out.println("salary:"+salary);
System.out.println("pos:"+raf.getFilePointer());
//读取入职日期
data = new byte[30];
raf.read(data);
String hiredate = new String(data).trim();
System.out.println("hiredate"+hiredate);
System.out.println("pos:"+raf.getFilePointer());
}*/
raf.close();
}
public static String readString(RandomAccessFile raf,int len) throws IOException {
byte[] date = new byte[len];
raf.read(date);
return new String(date).trim();
}
}