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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BufferedReader和BufferedWriter读写文件

發(fā)布時間:2025/4/16 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BufferedReader和BufferedWriter读写文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.創(chuàng)建Student類存儲每個學(xué)生信息,屬性(學(xué)號,姓名,出生日期,得分)
2.從c:/test/student.txt文件中讀取學(xué)生信息。如下:
?????? 學(xué)號,姓名,出生日期,得分
?????? 1,張三,1982-1-1,80
???????2,李四,1982-11-15,40
?????? 3,王五,1982-2-8,60
?????? 4,趙六,1982-7-5,70
?????? 5,小明,1981-12-21,70
?????? 6,李大嘴,1982-1-3,70
3.使用List存儲6名學(xué)生的信息。
4.使用集合排序,將學(xué)生信息按時得分從高到低排序,得分相同時按照出生日期升序排序。
5.輸出排序后的結(jié)果到c:/test/result.txt文件中,輸出格式與輸入格式相同,第一行是表頭。
6.在代碼中使用泛型,讀文本文件使用BufferedReader,寫文本文件使用BufferedWriter

解:
1.創(chuàng)建StudentInfo類,實(shí)現(xiàn)Comparable接口

package com.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StudentInfo implements Comparable<StudentInfo> { private String id; //學(xué)號 private String name; //學(xué)生姓名 private String birthday; //出生日期 private String score; //分?jǐn)?shù) 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 String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } /** * * {排序方法} * * @param objStu * @return * @author:LJ */ public int compareTo(StudentInfo objStu) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //得分相同時按照出生日期升序排序 if (this.score.equals(objStu.getScore())) { //格式化日期字符串 Date date1 = null; try { date1 = dateFormat.parse(this.birthday); } catch (ParseException e1) { e1.printStackTrace(); } Date date2 = null; try { date2 = dateFormat.parse(objStu.getBirthday()); } catch (ParseException e2) { e2.printStackTrace(); } //出生日期升序排序 return date1.compareTo(date2); } //得分從高到低排序 return objStu.getScore().compareTo(this.score); } }
2.讀寫文件類StudentFile.java

package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class StudentFile { /** * * {根據(jù)路徑讀取學(xué)生文件信息} * * @param path * @return List<StudentInfo> * @throws Exception * @author:LJ */ public List<StudentInfo> readFile(String path) throws Exception { List<StudentInfo> studentList = new ArrayList<StudentInfo>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(path)); String line = null; StudentInfo student = null; while ((line = br.readLine()) != null) { student = new StudentInfo(); //將字符串分割成字符串?dāng)?shù)組 String[] studentStr = line.split(","); student.setId(studentStr[0]); student.setName(studentStr[1]); student.setBirthday(studentStr[2]); student.setScore(studentStr[3]); studentList.add(student); } } catch (FileNotFoundException e) { throw new Exception("源文件未找到", e); } catch (IOException e) { throw new Exception("讀文件異常.", e); } finally {//資源關(guān)閉 if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return studentList; } /** * * {將學(xué)生信息寫入目標(biāo)文件} * * @param studentList * @param dstPath * @throws Exception * @author:LJ */ public void writeFile(List<StudentInfo> studentList, String dstPath) throws Exception { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(dstPath)); if (studentList != null && !studentList.isEmpty()) { for(StudentInfo stu:studentList) { bw.write(stu.getId()+","+stu.getName()+","+stu.getBirthday()+","+stu.getScore()); bw.newLine();//換行 } } bw.flush();//強(qiáng)制輸出緩沖區(qū)的內(nèi)容,避免數(shù)據(jù)緩存,造成文件寫入不完整的情況。 } catch (IOException e) { throw new Exception("目標(biāo)文件未找到", e); } finally { //資源關(guān)閉 if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
3.編寫main方法

package com.test; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test { /** * {main方法} * * @param args * @author:LJ * @throws Exception */ public static void main(String[] args) throws Exception { String srcPath = "C:" + File.separator + "test" + File.separator + "student.txt"; String dstPath = "C:" + File.separator + "test" + File.separator + "result.txt"; //從源文件讀取學(xué)生信息 StudentFile fileStu = new StudentFile(); List<StudentInfo> studentList = fileStu.readFile(srcPath); //臨時數(shù)組,作排序用 List<StudentInfo> tempList = new ArrayList<StudentInfo>(); for (int i = studentList.size()-1; i > 0; i--) { //將學(xué)生信息存入臨時數(shù)組,并從原數(shù)組中刪除,行標(biāo)題除外 tempList.add(studentList.get(i)); studentList.remove(i); } //對臨時數(shù)組進(jìn)行排序 Collections.sort(tempList); for (int i=0,n = tempList.size(); i < n; i++) { //將排序后數(shù)組追加到原數(shù)組中 studentList.add(tempList.get(i)); } //將學(xué)生信息寫入目標(biāo)文件 fileStu.writeFile(studentList, dstPath); } }

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的BufferedReader和BufferedWriter读写文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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