DataInput&DataOutput&RandomAccessFile

本文介绍 Java 中 DataInputStream 和 DataOutputStream 的使用方法,并通过一个 Employee 类实例演示如何利用这些类进行二进制数据的读写操作。同时,文章还展示了 RandomAccessFile 的随机访问特性及其在实际场景中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class DataInputStream extends FilterInputStream implements DataInput
public class DataOutputStream extends FilterOutputStream
implements DataOutput

可以看出此2类的设计还是用来对付二进制的,这两个类通常来说是配对使用的,这两类中定义的方法也是很配的。主要是用来对付java中的primitive data的,因为它们出现的频率过多了。
不同的primitive data有不同的方法。

RandomAccessFile extends Object implements DataInput DataOutput
但是它牛逼的地方在于可以随机访问,看他的方法
length()
seek()
skipByte()...
而且是很独立的一个类

还有就是GregorianCalendar 和 Date的使用,再研究!


package xml;
import java.util.*;
import java.io.*;

public class RandomFileTest {
public static void main(String[] args){
Employee[] employees=new Employee[3];
employees[0]=new Employee("a",2500,1989,1,1);
employees[1]=new Employee("b",2500,1989,1,1);
employees[2]=new Employee("c",2500,1989,1,1);

try{
DataOutputStream out=new DataOutputStream(new FileOutputStream("randomFile.txt"));

for(Employee e : employees){
e.writeData(out);
}
//关闭
out.close();

//以只读方式打开文件
RandomAccessFile in=new RandomAccessFile("randomFile.txt","r");
//有多少个记录,in.length()是个double
int n=(int)in.length()/Employee.RECORD_SIZE;
Employee[] newStaff=new Employee[n];
for(int i=0;i<n;i++){
newStaff[i]=new Employee();
//读下一条记录
in.seek(i*Employee.RECORD_SIZE);
newStaff[i].readData(in);
}
in.close();

//我可定义了toString()方法哦!
for(Employee e : newStaff)
System.out.println(e);
}catch(IOException e){}
}
}
class Employee{
private String name;
private double salary;
private Date hireDay;
//40代表40个char
public static final int NAME_SIZE=40;
//每个记录占多杀个字节
public static final int RECORD_SIZE=NAME_SIZE*2+8+3*4;
public Employee(){}
public Employee(String n,double s,int y,int m,int d){
this.name=n;
this.salary=s;
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
this.hireDay=calendar.getTime();
}
public String getName(){
return this.name;
}
public double getSalary(){
return this.salary;
}
public Date getHireDay(){
return this.hireDay;
}
public String toString(){
return getClass().getName()+"[name="+name+",salary="+this.salary+",hireday="+this.hireDay+"]";
}

//parameter is interface DataOutput
public void writeData(DataOutput out)throws IOException{
DataIO.writeFixString(this.name,this.NAME_SIZE,out);
out.writeDouble(this.salary);

GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(this.hireDay);

out.writeInt(calendar.get(Calendar.YEAR));
out.writeInt(calendar.get(Calendar.MONTH)+1);
out.writeInt(calendar.get(Calendar.DAY_OF_MONTH));
}

public void readData(DataInput in)throws IOException{
this.name=DataIO.readFixString(this.NAME_SIZE, in);
this.salary=in.readDouble();
int y=in.readInt();
int m=in.readInt();
int d=in.readInt();
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
this.hireDay=calendar.getTime();
}
}

//因为DataOutput DataInput在处理字符串的时候,在文本中占用的byte难以控制,所以写了这个静态工具类
class DataIO{

public static void writeFixString(String name,int size,DataOutput out)throws IOException{
for(int i=0;i<size;i++){
char c=0;
if(i<name.length())
c=name.charAt(i);
out.writeChar(c);
}
}

public static String readFixString(int size,DataInput in)throws IOException{
StringBuilder sb=new StringBuilder(size);
boolean more=true;
int i=0;
while(i<size&&more){
char c=in.readChar();
if(c==0)
more=false;
else
sb.append(c);
i++;
}
in.skipBytes(2*(size-i));
return sb.toString();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值