流是一種抽象概念,它代表了數(shù)據(jù)的無結(jié)構(gòu)化傳遞。按照流的方式進行輸入輸出,數(shù)據(jù)被當(dāng)成無結(jié)構(gòu)的字節(jié)序或字符序列。從流中取得數(shù)據(jù)的操作稱為提取操作,而向流中添加數(shù)據(jù)的操作稱為插入操作。用來進行輸入輸出操作的流就稱為IO流。換句話說,IO流就是以流的方式進行輸入輸出。

本文要為大家介紹 IO流案例,主要內(nèi)容包括案例需求、步驟分析、代碼實現(xiàn)等等,現(xiàn)在一起來看看吧!
1、集合到文件數(shù)據(jù)排序
(1)案例需求
鍵盤錄入5個學(xué)生信息(姓名,語文成績,數(shù)學(xué)成績,英語成績)。要求按照成績總分從高到低寫入文本文件。格式:姓名,語文成績,數(shù)學(xué)成績,英語成績? 舉例:林青霞,98,99,100
(2)分析步驟
a.定義學(xué)生類
b.創(chuàng)建TreeSet集合,通過比較器排序進行排序
c.鍵盤錄入學(xué)生數(shù)據(jù)
d.創(chuàng)建學(xué)生對象,把鍵盤錄入的數(shù)據(jù)對應(yīng)賦值給學(xué)生對象的成員變量
e.把學(xué)生對象添加到TreeSet集合
f.創(chuàng)建字符緩沖輸出流對象
g.遍歷集合,得到每一個學(xué)生對象
h.把學(xué)生對象的數(shù)據(jù)拼接成指定格式的字符串
i.調(diào)用字符緩沖輸出流對象的方法寫數(shù)據(jù)
j.釋放資源
(3)代碼實現(xiàn)
學(xué)生類:
java
public class Student {
// 姓名
private String name;
// 語文成績
private int chinese;
// 數(shù)學(xué)成績
private int math;
// 英語成績
private int english;
public Student() {
super();
}
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum() {
return this.chinese + this.math + this.english;
}
}
```
測試類:
```java
public class TreeSetToFileDemo {
public static void main(String[] args) throws IOException {
//創(chuàng)建TreeSet集合,通過比較器排序進行排序
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//成績總分從高到低
int num = s2.getSum() - s1.getSum();
//次要條件
int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
int num4 = num3 == 0 ? s1.getName().compareTo(s2.getName()) : num3;
return num4;
}
});
//鍵盤錄入學(xué)生數(shù)據(jù)
for (int i = 0; i < 5; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入第" + (i + 1) + "個學(xué)生信息:");
System.out.println("姓名:");
String name = sc.nextLine();
System.out.println("語文成績:");
int chinese = sc.nextInt();
System.out.println("數(shù)學(xué)成績:");
int math = sc.nextInt();
System.out.println("英語成績:");
int english = sc.nextInt();
//創(chuàng)建學(xué)生對象,把鍵盤錄入的數(shù)據(jù)對應(yīng)賦值給學(xué)生對象的成員變量
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
//把學(xué)生對象添加到TreeSet集合
ts.add(s);
}
//創(chuàng)建字符緩沖輸出流對象
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\ts.txt"));
//遍歷集合,得到每一個學(xué)生對象
for (Student s : ts) {
//把學(xué)生對象的數(shù)據(jù)拼接成指定格式的字符串
//格式:姓名,語文成績,數(shù)學(xué)成績,英語成績
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append(",").append(s.getSum());
// 調(diào)用字符緩沖輸出流對象的方法寫數(shù)據(jù)
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//釋放資源
bw.close();
}
}
以上就是Java基礎(chǔ)學(xué)習(xí)筆記之IO流案例的全部內(nèi)容,大家都明白了嗎?
網(wǎng)站名稱:Java中的IO流介紹-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article32/dgpspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站策劃、動態(tài)網(wǎng)站、微信小程序、外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容