BufferedReader和BufferedWriter读写文件
生活随笔
收集整理的這篇文章主要介紹了
BufferedReader和BufferedWriter读写文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.test; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class StudentInfo implements Comparable<StudentInfo>
{ private String id; //學號 private String name; //學生姓名 private String birthday; //出生日期 private String score; //分數 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 { /** * * {根據路徑讀取學生文件信息} * * @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(); //將字符串分割成字符串數組 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 {//資源關閉 if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return studentList; } /** * * {將學生信息寫入目標文件} * * @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();//強制輸出緩沖區的內容,避免數據緩存,造成文件寫入不完整的情況。 } catch (IOException e) { throw new Exception("目標文件未找到", e); } finally { //資源關閉 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"; //從源文件讀取學生信息 StudentFile fileStu = new StudentFile(); List<StudentInfo> studentList = fileStu.readFile(srcPath); //臨時數組,作排序用 List<StudentInfo> tempList = new ArrayList<StudentInfo>(); for (int i = studentList.size()-1; i > 0; i--) { //將學生信息存入臨時數組,并從原數組中刪除,行標題除外 tempList.add(studentList.get(i)); studentList.remove(i); } //對臨時數組進行排序 Collections.sort(tempList); for (int i=0,n = tempList.size(); i < n; i++) { //將排序后數組追加到原數組中 studentList.add(tempList.get(i)); } //將學生信息寫入目標文件 fileStu.writeFile(studentList, dstPath); } }
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
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 { /** * * {根據路徑讀取學生文件信息} * * @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(); //將字符串分割成字符串數組 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 {//資源關閉 if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return studentList; } /** * * {將學生信息寫入目標文件} * * @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();//強制輸出緩沖區的內容,避免數據緩存,造成文件寫入不完整的情況。 } catch (IOException e) { throw new Exception("目標文件未找到", e); } finally { //資源關閉 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"; //從源文件讀取學生信息 StudentFile fileStu = new StudentFile(); List<StudentInfo> studentList = fileStu.readFile(srcPath); //臨時數組,作排序用 List<StudentInfo> tempList = new ArrayList<StudentInfo>(); for (int i = studentList.size()-1; i > 0; i--) { //將學生信息存入臨時數組,并從原數組中刪除,行標題除外 tempList.add(studentList.get(i)); studentList.remove(i); } //對臨時數組進行排序 Collections.sort(tempList); for (int i=0,n = tempList.size(); i < n; i++) { //將排序后數組追加到原數組中 studentList.add(tempList.get(i)); } //將學生信息寫入目標文件 fileStu.writeFile(studentList, dstPath); } }
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的BufferedReader和BufferedWriter读写文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: File类 读取文件
- 下一篇: Java数组的基本操作方法整理