Java高级语法笔记-文件操作-链表的存储

代码如下:

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+")";
	}
	
	
}

运行截图如下:



评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值