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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IO流与对象结合

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IO流与对象结合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package IODemo;import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Iterator;/*** @author Alina* @date 2021年11月08日 9:13 下午* 把Arraylist 里存儲的內容存儲到本地文件* 1.遍歷集合。遍歷一次存儲一次*/ public class IOtest {public static void main(String[] args)throws Exception {CreadArraylist();readtxt();}// 創建數據存入集合public static void CreadArraylist()throws Exception{ArrayList<People> arrayListOne = new ArrayList<>();arrayListOne.add(new People("001","張三",18) );arrayListOne.add(new People("002","李四",20) );arrayListOne.add(new People("003","王二",21) );arrayListOne.add(new People("004","麻子",94) );arrayListOne.add(new People("005","李四",29) );arrayListOne.add(new People("006","王五",9) );//創建輸出流BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src/IODemo/people.txt"));//創建集合的迭代器Iterator<People> it = arrayListOne.iterator();while (it.hasNext()){bos.write(it.next().toString().getBytes(StandardCharsets.UTF_8));bos.write("\n".getBytes(StandardCharsets.UTF_8));}bos.close();}//讀取文件中內容public static void readtxt() throws Exception{//創建讀入流BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src/IODemo/people.txt"));//創建集合ArrayList<People> read_arraylist = new ArrayList<>();byte [] bytes = new byte[1024];int num = 0;while ((num=bis.read(bytes)) != -1){System.out.print(new String(bytes,0,num));}}} package IODemo;/*** @author Alina* @date 2021年11月08日 9:22 下午*/ public class People {String ID;String name ;int age;public People(String ID, String name, int age) {this.ID = ID;this.name = name;this.age = age;}public String toString(){return getID()+" "+getName()+" "+getAge();}public String getID() {return ID;}public void setID(String ID) {this.ID = ID;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

總結

以上是生活随笔為你收集整理的IO流与对象结合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。