代码如下:
AfByteBuffer.java
package my;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
public class HelloWorld
{
public static void write(ArrayList<Student> ss) throws Exception
{
// 文件
File filename = new File("c:/bbbb");
// 创建文件, 写入数据
FileOutputStream fout = new FileOutputStream(filename);
byte[] buf = new byte[1024];
ByteBuffer dstbuf = ByteBuffer.wrap(buf);
for(Student e : ss)
{
// 将student编码到buf
dstbuf.position(0); // 重置
dstbuf.putInt(e.id);
AfByteBuffer.putString(dstbuf, e.name);
AfByteBuffer.putString(dstbuf, e.phone);
int N = dstbuf.position();
// 写入到文件中
fout.write(AfByteBuffer.asShort((short)N)); // 先写长度
fout.write(buf, 0, N); // 一个学生的编码数据
}
fout.close();
}
public static ArrayList<Student> read() throws Exception
{
ArrayList<Student> ss = new ArrayList<Student>();
// 文件
File filename = new File("c:/bbbb");
FileInputStream fin = new FileInputStream(filename);
byte[] buf = new byte[1024];
while(true)
{
// 先读2个字节
int n = fin.read(buf, 0, 2);
if(n != 2) break;
// 再读一个学生的信息数据
int total = AfByteBuffer.asShort(buf, 0);
n = fin.read(buf, 0, total);
if(n != total) break;
// 解码,还原为Student
ByteBuffer srcbuf = ByteBuffer.wrap(buf);
Student e = new Student();
e.id = srcbuf.getInt();
e.name = AfByteBuffer.getString(srcbuf);
e.phone = AfByteBuffer.getString(srcbuf);
ss.add(e);
}
fin.close();
return ss;
}
public static void main(String[] args){
ArrayList<Student> ss=new ArrayList<Student>();
ss.add(new Student(1000,"qiuqiu","14523623254"));
ss.add(new Student(1001,"xixi","18656985685"));
ss.add(new Student(1002,"tuitui","13998689521"));
ss.add(new Student(1003,"meiye","13895654123"));
try {
write(ss);
ArrayList<Student> results=read();
for(Student e:results) {
System.out.println("got:"+e);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
HelloWorld.java
package my;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
public class HelloWorld
{
public static void write(ArrayList<Student> ss) throws Exception
{
// 文件
File filename = new File("c:/bbbb");
// 创建文件, 写入数据
FileOutputStream fout = new FileOutputStream(filename);
byte[] buf = new byte[1024];
ByteBuffer dstbuf = ByteBuffer.wrap(buf);
for(Student e : ss)
{
// 将student编码到buf
dstbuf.position(0); // 重置
dstbuf.putInt(e.id);
AfByteBuffer.putString(dstbuf, e.name);
AfByteBuffer.putString(dstbuf, e.phone);
int N = dstbuf.position();
// 写入到文件中
fout.write(AfByteBuffer.asShort((short)N)); // 先写长度
fout.write(buf, 0, N); // 一个学生的编码数据
}
fout.close();
}
public static ArrayList<Student> read() throws Exception
{
ArrayList<Student> ss = new ArrayList<Student>();
// 文件
File filename = new File("c:/bbbb");
FileInputStream fin = new FileInputStream(filename);
byte[] buf = new byte[1024];
while(true)
{
// 先读2个字节
int n = fin.read(buf, 0, 2);
if(n != 2) break;
// 再读一个学生的信息数据
int total = AfByteBuffer.asShort(buf, 0);
n = fin.read(buf, 0, total);
if(n != total) break;
// 解码,还原为Student
ByteBuffer srcbuf = ByteBuffer.wrap(buf);
Student e = new Student();
e.id = srcbuf.getInt();
e.name = AfByteBuffer.getString(srcbuf);
e.phone = AfByteBuffer.getString(srcbuf);
ss.add(e);
}
fin.close();
return ss;
}
public static void main(String[] args){
ArrayList<Student> ss=new ArrayList<Student>();
ss.add(new Student(1000,"qiuqiu","14523623254"));
ss.add(new Student(1001,"xixi","18656985685"));
ss.add(new Student(1002,"tuitui","13998689521"));
ss.add(new Student(1003,"meiye","13895654123"));
try {
write(ss);
ArrayList<Student> results=read();
for(Student e:results) {
System.out.println("got:"+e);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
Student.java
package my;
public class Student
{
int id;
String name;
String phone;
public Student() {
}
public Student(int id,String name,String phone) {
this.id=id;
this.name=name;
this.phone=phone;
}
@Override
public String toString(){
return "("+id+","+name+","+phone+")";
}
}
运行截图如下: