Java高级语法笔记-文件操作-链表的存储
生活随笔
收集整理的這篇文章主要介紹了
Java高级语法笔记-文件操作-链表的存储
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼如下:
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");// 創(chuàng)建文件, 寫(xiě)入數(shù)據(jù)FileOutputStream fout = new FileOutputStream(filename);byte[] buf = new byte[1024];ByteBuffer dstbuf = ByteBuffer.wrap(buf);for(Student e : ss){// 將student編碼到bufdstbuf.position(0); // 重置dstbuf.putInt(e.id);AfByteBuffer.putString(dstbuf, e.name);AfByteBuffer.putString(dstbuf, e.phone);int N = dstbuf.position();// 寫(xiě)入到文件中fout.write(AfByteBuffer.asShort((short)N)); // 先寫(xiě)長(zhǎng)度f(wàn)out.write(buf, 0, N); // 一個(gè)學(xué)生的編碼數(shù)據(jù)}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個(gè)字節(jié)int n = fin.read(buf, 0, 2);if(n != 2) break;// 再讀一個(gè)學(xué)生的信息數(shù)據(jù)int total = AfByteBuffer.asShort(buf, 0);n = fin.read(buf, 0, total);if(n != total) break;// 解碼,還原為StudentByteBuffer 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");// 創(chuàng)建文件, 寫(xiě)入數(shù)據(jù)FileOutputStream fout = new FileOutputStream(filename);byte[] buf = new byte[1024];ByteBuffer dstbuf = ByteBuffer.wrap(buf);for(Student e : ss){// 將student編碼到bufdstbuf.position(0); // 重置dstbuf.putInt(e.id);AfByteBuffer.putString(dstbuf, e.name);AfByteBuffer.putString(dstbuf, e.phone);int N = dstbuf.position();// 寫(xiě)入到文件中fout.write(AfByteBuffer.asShort((short)N)); // 先寫(xiě)長(zhǎng)度f(wàn)out.write(buf, 0, N); // 一個(gè)學(xué)生的編碼數(shù)據(jù)}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個(gè)字節(jié)int n = fin.read(buf, 0, 2);if(n != 2) break;// 再讀一個(gè)學(xué)生的信息數(shù)據(jù)int total = AfByteBuffer.asShort(buf, 0);n = fin.read(buf, 0, total);if(n != total) break;// 解碼,還原為StudentByteBuffer 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;}@Overridepublic String toString(){return "("+id+","+name+","+phone+")";}}
運(yùn)行截圖如下:
總結(jié)
以上是生活随笔為你收集整理的Java高级语法笔记-文件操作-链表的存储的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 大三软件工程小项目-小技术集合-读取XM
- 下一篇: android 返回销毁活动,andro