Java基礎(chǔ)學(xué)習(xí)之IO流應(yīng)用案例詳解
一、點(diǎn)名器
需求:
我有一個(gè)文件里面存儲(chǔ)了班級(jí)同學(xué)的姓名,每一個(gè)姓名占一行,要求通過程序?qū)崿F(xiàn)隨機(jī)點(diǎn)名器
實(shí)現(xiàn)步驟:
- 創(chuàng)建字符緩沖輸入流對象
- 創(chuàng)建ArrayList集合對象
- 調(diào)用字符緩沖流對象的方法讀數(shù)據(jù)
- 把讀取到的字符串?dāng)?shù)據(jù)存儲(chǔ)到集合中
- 釋放資源
- 使用Randam產(chǎn)生一個(gè)隨機(jī)數(shù),隨機(jī)數(shù)的范圍在:[0,集合的長度]
- 把第6步產(chǎn)生的隨機(jī)數(shù)作為索引到ArrayList集合中獲取值
- 把第7步得到的數(shù)據(jù)輸出在控制臺(tái)
代碼實(shí)現(xiàn):
public class CallNameDemo {
public static void main(String[] args) throws IOException {
//創(chuàng)建字符緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\names.txt"));
//創(chuàng)建ArrayList集合對象
ArrayList<String> array = new ArrayList<String>();
//調(diào)用字符緩沖輸入流對象的方法讀數(shù)據(jù)
String line;
while ((line=br.readLine())!=null) {
//把讀取到的字符串?dāng)?shù)據(jù)存儲(chǔ)到集合中
array.add(line);
}
//釋放資源
br.close();
//使用Random產(chǎn)生一個(gè)隨機(jī)數(shù),隨機(jī)數(shù)的范圍在:[0,集合的長度)
Random r = new Random();
int index = r.nextInt(array.size());
//把第6步產(chǎn)生的隨機(jī)數(shù)作為索引到ArrayList集合中獲取值
String name = array.get(index);
//把第7步得到的數(shù)據(jù)輸出在控制臺(tái)
System.out.println("幸運(yùn)者是:" + name);
}
}
二、集合到文件
需求:
把ArrayList集合中的學(xué)生數(shù)據(jù)寫入到文本文件。要求:每一個(gè)學(xué)生對象的數(shù)據(jù)作為文件中的一行數(shù)據(jù),格式:學(xué)號(hào),姓名,年齡,居住地 舉例:itheima001,小林,20,青島
實(shí)現(xiàn)步驟:
- 定義學(xué)生類
- 創(chuàng)建ArrayList集合
- 創(chuàng)建學(xué)生對象
- 把學(xué)生對象添加到集合中
- 創(chuàng)建字符緩沖輸出流對象
- 遍歷集合,得到每一個(gè)學(xué)生對象
- 把學(xué)生對象的數(shù)據(jù)拼接成指定格式的字符串
- 調(diào)用字符緩沖輸出流對象的方法寫數(shù)據(jù)
- 釋放資源
代碼實(shí)現(xiàn):
學(xué)生類
public class Student {
private String sid;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String sid, String name, int age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
測試類:
public class ArrayListToFileDemo {
public static void main(String[] args) throws IOException {
//創(chuàng)建ArrayList集合
ArrayList<Student> array = new ArrayList<Student>();
//創(chuàng)建學(xué)生對象
Student s1 = new Student("itheima001", "小林", 20, "西安");
Student s2 = new Student("itheima002", "小張", 25, "武漢");
Student s3 = new Student("itheima003", "小王", 23, "鄭州");
//把學(xué)生對象添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
//創(chuàng)建字符緩沖輸出流對象
BufferedWriter bw = new BufferedWriter(new FileWriter("myCharStream\\students.txt"));
//遍歷集合,得到每一個(gè)學(xué)生對象
for (Student s : array) {
//把學(xué)生對象的數(shù)據(jù)拼接成指定格式的字符串
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.ge tAge()).append(",").append(s.getAddress());
//調(diào)用字符緩沖輸出流對象的方法寫數(shù)據(jù)
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//釋放資源
bw.close();
}
}
三、文件到集合
需求:
把文本文件中的數(shù)據(jù)讀取到集合中,并遍歷集合。要求:文件中每一行數(shù)據(jù)是一個(gè)學(xué)生對象的成員變量值,舉例:itheima001,小林,23,青島
實(shí)現(xiàn)步驟:
- 定義學(xué)生類
- 創(chuàng)建字符緩沖輸入流對象
- 創(chuàng)建ArrayList集合對象
- 調(diào)用字符緩沖輸入流對象的方法讀數(shù)據(jù)
- 把讀取到的字符串?dāng)?shù)據(jù)用split()進(jìn)行分隔,得到一個(gè)字符串?dāng)?shù)組
- 創(chuàng)建學(xué)生對象
- 把字符串?dāng)?shù)組中的每一個(gè)元素取出來對應(yīng)的賦值給學(xué)生對象的成員變量值
- 把學(xué)生對象添加到集合
- 釋放資源
- 遍歷集合
代碼實(shí)現(xiàn):
學(xué)生類同上
測試類:
public class FileToArrayListDemo {
public static void main(String[] args) throws IOException {
//創(chuàng)建字符緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("myCharStream\\students.txt"));
//創(chuàng)建ArrayList集合對象
ArrayList<Student> array = new ArrayList<Student>();
//調(diào)用字符緩沖輸入流對象的方法讀數(shù)據(jù)
String line;
while ((line = br.readLine()) != null) {
//把讀取到的字符串?dāng)?shù)據(jù)用split()進(jìn)行分割,得到一個(gè)字符串?dāng)?shù)組
String[] strArray = line.split(",");
//創(chuàng)建學(xué)生對象
Student s = new Student();
//把字符串?dāng)?shù)組中的每一個(gè)元素取出來對應(yīng)的賦值給學(xué)生對象的成員變量值
//itheima001,林青霞,30,西安
s.setSid(strArray[0]);
s.setName(strArray[1]);
s.setAge(Integer.parseInt(strArray[2]));
s.setAddress(strArray[3]);
//把學(xué)生對象添加到集合
array.add(s);
}
//釋放資源
br.close();
//遍歷集合
for (Student s : array) {
System.out.println(s.getSid() + "," + s.getName() + "," +
s.getAge() + "," + s.getAddress());
}
}
}到此這篇關(guān)于Java基礎(chǔ)學(xué)習(xí)之IO流應(yīng)用案例詳解的文章就介紹到這了,更多相關(guān)Java IO流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring MVC 更靈活的控制 json 返回問題(自定義過濾字段)
本篇文章主要介紹了Spring MVC 更靈活的控制 json 返回問題(自定義過濾字段),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Java toString方法重寫工具之ToStringBuilder案例詳解
這篇文章主要介紹了Java toString方法重寫工具之ToStringBuilder案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
java9的JShell小工具和編譯器兩種自動(dòng)優(yōu)化方法
這篇文章主要介紹了java9的JShell小工具和編譯器兩種自動(dòng)優(yōu)化方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作
這篇文章主要介紹了在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java內(nèi)存泄漏問題處理方法經(jīng)驗(yàn)總結(jié)
今天小編就為大家分享一篇關(guān)于Java內(nèi)存泄漏問題處理方法經(jīng)驗(yàn)總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

