日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2025/3/15 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java高级语法笔记-文件操作-链表的存储 小編覺得挺不錯(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)建文件, 寫入數(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();// 寫入到文件中fout.write(AfByteBuffer.asShort((short)N)); // 先寫長度fout.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)建文件, 寫入數(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();// 寫入到文件中fout.write(AfByteBuffer.asShort((short)N)); // 先寫長度fout.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高级语法笔记-文件操作-链表的存储的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。