生活随笔
收集整理的這篇文章主要介紹了
Java基础day18
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java基礎(chǔ)day18
- Java基礎(chǔ)day18-字節(jié)緩沖流&字符流
- 1.字節(jié)緩沖流
- 1.1字節(jié)緩沖流構(gòu)造方法
- 1.2字節(jié)流復(fù)制視頻
- 2.字符流
- 2.1為什么會(huì)出現(xiàn)字符流
- 2.2編碼表
- 2.3字符串中的編碼解碼問題
- 2.4字符流中的編碼解碼問題
- 2.5字符流寫數(shù)據(jù)的5種方式【應(yīng)用】
- 2.6字符流讀數(shù)據(jù)的2種方式
- 2.7字符流復(fù)制Java文件
- 2.8字符流復(fù)制Java文件改進(jìn)版
- 2.9字符緩沖流
- 2.10字符緩沖流復(fù)制Java文件
- 2.11字符緩沖流特有功能
- 2.12字符緩沖流特有功能復(fù)制Java文件
- 2.13IO流小結(jié)
- 3練習(xí)案例
- 3.1集合到文件
- 3.2文件到集合
- 3.3點(diǎn)名器
- 3.4集合到文件改進(jìn)版
- 3.5文件到集合改進(jìn)版
Java基礎(chǔ)day18-字節(jié)緩沖流&字符流
1.字節(jié)緩沖流
1.1字節(jié)緩沖流構(gòu)造方法
- 字節(jié)緩沖流介紹
lBufferOutputStream:該類實(shí)現(xiàn)緩沖輸出流。 通過設(shè)置這樣的輸出流,應(yīng)用程序可以向底層輸出流寫入字節(jié),而不必為寫入的每個(gè)字節(jié)導(dǎo)致底層系統(tǒng)的調(diào)用
lBufferedInputStream:創(chuàng)建BufferedInputStream將創(chuàng)建一個(gè)內(nèi)部緩沖區(qū)數(shù)組。 當(dāng)從流中讀取或跳過字節(jié)時(shí),內(nèi)部緩沖區(qū)將根據(jù)需要從所包含的輸入流中重新填充,一次很多字節(jié) - 構(gòu)造方法:
方法名說(shuō)明
| BufferedOutputStream(OutputStream out) | 創(chuàng)建字節(jié)緩沖輸出流對(duì)象 |
| BufferedInputStream(InputStream in) | 創(chuàng)建字節(jié)緩沖輸入流對(duì)象 |
import java
.io
.*
;public class test1 {public static void main(String
[] args
) throws IOException
{FileOutputStream fos
= new FileOutputStream("src\\fos.txt");BufferedOutputStream bos
= new BufferedOutputStream(fos
);bos
.write("hello\r\n".getBytes());bos
.write("world\r\n".getBytes());bos
.close();FileInputStream fis
= new FileInputStream("src\\fos.txt");BufferedInputStream bis
= new BufferedInputStream(fis
);byte[] bys
= new byte[1024];int len
;while ((len
= bis
.read(bys
))!=-1){System
.out
.print(new String(bys
,0,len
));}bos
.close();}
}
1.2字節(jié)流復(fù)制視頻
- 案例需求
把“E:\itcast\字節(jié)流復(fù)制圖片.avi”復(fù)制到模塊目錄下的“字節(jié)流復(fù)制圖片.avi” - 實(shí)現(xiàn)步驟
根據(jù)數(shù)據(jù)源創(chuàng)建字節(jié)輸入流對(duì)象
根據(jù)目的地創(chuàng)建字節(jié)輸出流對(duì)象
讀寫數(shù)據(jù),復(fù)制視頻
釋放資源 - 代碼實(shí)現(xiàn)
import java
.io
.*
;public class test2 {public static void main(String
[] args
) throws IOException
{long starttime
= System
.currentTimeMillis();method1();
long endtime
= System
.currentTimeMillis();System
.out
.println("共耗時(shí):" + (endtime
- starttime
) + "毫秒");}public static void method1() throws IOException
{FileInputStream fis
= new FileInputStream("E:\\itcast\\字節(jié)流復(fù)制圖片.avi");FileOutputStream fos
= new FileOutputStream("字節(jié)流復(fù)制圖片.avi");int by
;while ((by
= fis
.read())!=-1){fos
.write(by
);}fos
.close();fis
.close();}public static void method2() throws IOException
{FileInputStream fis
= new FileInputStream("E:\\itcast\\字節(jié)流復(fù)制圖片.avi");FileOutputStream fos
= new FileOutputStream("字節(jié)流復(fù)制圖片.avi");byte[] bys
= new byte[1024];int len
;while ((len
= fis
.read(bys
))!=-1){fos
.write(bys
,0,len
);}fos
.close();fis
.close();}public static void method3() throws IOException
{FileInputStream fis
= new FileInputStream("E:\\itcast\\字節(jié)流復(fù)制圖片.avi");BufferedInputStream bis
= new BufferedInputStream(fis
);FileOutputStream fos
= new FileOutputStream("字節(jié)流復(fù)制圖片.avi");BufferedOutputStream bos
= new BufferedOutputStream(fos
);int by
;while ((by
= bis
.read())!=-1){bos
.write(by
);}bos
.close();bis
.close();}public static void method4() throws IOException
{FileInputStream fis
= new FileInputStream("E:\\itcast\\字節(jié)流復(fù)制圖片.avi");BufferedInputStream bis
= new BufferedInputStream(fis
);FileOutputStream fos
= new FileOutputStream("字節(jié)流復(fù)制圖片.avi");BufferedOutputStream bos
= new BufferedOutputStream(fos
);byte[] bys
= new byte[1024];int len
;while ((len
= bis
.read(bys
))!=-1){fos
.write(bys
,0,len
);}bos
.close();bis
.close();}
}
2.字符流
2.1為什么會(huì)出現(xiàn)字符流
- 字符流的介紹
由于字節(jié)流操作中文不是特別的方便,所以Java就提供字符流
字符流 = 字節(jié)流 + 編碼表 - 中文的字節(jié)存儲(chǔ)方式
用字節(jié)流復(fù)制文本文件時(shí),文本文件也會(huì)有中文,但是沒有問題,原因是最終底層操作會(huì)自動(dòng)進(jìn)行字節(jié)拼接成中文,如何識(shí)別是中文的呢?
漢字在存儲(chǔ)的時(shí)候,無(wú)論選擇哪種編碼存儲(chǔ),第一個(gè)字節(jié)都是負(fù)數(shù)
2.2編碼表
- 什么是字符集
是一個(gè)系統(tǒng)支持的所有字符的集合,包括各國(guó)家文字、標(biāo)點(diǎn)符號(hào)、圖形符號(hào)、數(shù)字等
l計(jì)算機(jī)要準(zhǔn)確的存儲(chǔ)和識(shí)別各種字符集符號(hào),就需要進(jìn)行字符編碼,一套字符集必然至少有一套字符編碼。
常見字符集有ASCII字符集、GBXXX字符集、Unicode字符集等
- 常見的字符集ASCII字符集:lASCII:是基于拉丁字母的一套電腦編碼系統(tǒng),用于顯示現(xiàn)代英語(yǔ),主要包括控制字符(回車鍵、退格、換行鍵等)和可顯示字符(英文大小寫字符、阿拉伯?dāng)?shù)字和西文符號(hào))基本的ASCII字符集,使用7位表示一個(gè)字符,共128字符。ASCII的擴(kuò)展字符集使用8位表示一個(gè)字符,共256字符,方便支持歐洲常用字符。是一個(gè)系統(tǒng)支持的所有字符的集合,包括各國(guó)家文字、標(biāo)點(diǎn)符號(hào)、圖形符號(hào)、數(shù)字等GBXXX字符集:GBK:最常用的中文碼表。是在GB2312標(biāo)準(zhǔn)基礎(chǔ)上的擴(kuò)展規(guī)范,使用了雙字節(jié)編碼方案,共收錄了21003個(gè)漢字,完全兼容GB2312標(biāo)準(zhǔn),同時(shí)支持繁體漢字以及日韓漢字等Unicode字符集:UTF-8編碼:可以用來(lái)表示Unicode標(biāo)準(zhǔn)中任意字符,它是電子郵件、網(wǎng)頁(yè)及其他存儲(chǔ)或傳送文字的應(yīng)用中,優(yōu)先采用的編碼?;ヂ?lián)網(wǎng)工程工作小組(IETF)要求所有互聯(lián)網(wǎng)協(xié)議都必須支持UTF-8編碼。它使用一至四個(gè)字節(jié)為每個(gè)字符編碼編碼規(guī)則:128個(gè)US-ASCII字符,只需一個(gè)字節(jié)編碼拉丁文等字符,需要二個(gè)字節(jié)編碼大部分常用字(含中文),使用三個(gè)字節(jié)編碼其他極少使用的Unicode輔助字符,使用四字節(jié)編碼
2.3字符串中的編碼解碼問題
方法名說(shuō)明
| byte[] getBytes() | 使用平臺(tái)的默認(rèn)字符集將該 String編碼為一系列字節(jié) |
| byte[] getBytes(String charsetName) | 使用指定的字符集將該 String編碼為一系列字節(jié) |
| String(byte[] bytes) | 使用平臺(tái)的默認(rèn)字符集解碼指定的字節(jié)數(shù)組來(lái)創(chuàng)建字符串 |
| String(byte[] bytes, String charsetName) | 通過指定的字符集解碼指定的字節(jié)數(shù)組來(lái)創(chuàng)建字符串 |
import java
.io
.IOException
;
import java
.util
.Arrays
;public class test1 {public static void main(String
[] args
) throws IOException
{String s
= "中國(guó)";byte[] bys
= s
.getBytes("GBK"); System
.out
.println(Arrays
.toString(bys
));String ss
= new String(bys
,"GBK");System
.out
.println(ss
);}
}
2.4字符流中的編碼解碼問題
- 字符流中和編碼解碼問題相關(guān)的兩個(gè)類
InputStreamReader:是從字節(jié)流到字符流的橋梁
它讀取字節(jié),并使用指定的編碼將其解碼為字符
它使用的字符集可以由名稱指定,也可以被明確指定,或者可以接受平臺(tái)的默認(rèn)字符集
OutputStreamWriter:是從字符流到字節(jié)流的橋梁
是從字符流到字節(jié)流的橋梁,使用指定的編碼將寫入的字符編碼為字節(jié)
它使用的字符集可以由名稱指定,也可以被明確指定,或者可以接受平臺(tái)的默認(rèn)字符集 - 構(gòu)造方法
方法名說(shuō)明
| InputStreamReader(InputStream in) | 使用默認(rèn)字符編碼創(chuàng)建InputStreamReader對(duì) 象 |
| InputStreamReader(InputStream in,String chatset) | 使用指定的字符編碼創(chuàng)建InputStreamReader對(duì)象 |
| OutputStreamWriter(OutputStream out) | 使用默認(rèn)字符編碼創(chuàng)建OutputStreamWriter對(duì) 象 |
| OutputStreamWriter(OutputStream out,String charset) | 使用指定的字符編碼創(chuàng)建OutputStreamWriter對(duì)象 |
import java
.io
.*
;public class test2 {public static void main(String
[] args
) throws IOException
{OutputStreamWriter osw
= new OutputStreamWriter(new FileOutputStream("src\\osw.txt"),"GBK");osw
.write("中國(guó)");osw
.close();InputStreamReader isr
= new InputStreamReader(new FileInputStream("src\\osw.txt"),"GBK");int ch
;while ((ch
=isr
.read())!=-1) {System
.out
.print((char)ch
);}isr
.close();}
}
2.5字符流寫數(shù)據(jù)的5種方式【應(yīng)用】
方法名說(shuō)明
| void write(int c) | 寫一個(gè)字符 |
| void write(char[] cbuf) | 寫入一個(gè)字符數(shù)組 |
| void write(char[] cbuf, int off, int len) | 寫入字符數(shù)組的一部分 |
| void write(String str) | 寫一個(gè)字符串 |
| void write(String str, int off, int len) | 寫一個(gè)字符串的一部分 |
方法名說(shuō)明
| flush() | 刷新流,之后還可以繼續(xù)寫數(shù)據(jù) |
| close() | 關(guān)閉流,釋放資源,但是在關(guān)閉之前會(huì)先刷新流。一旦關(guān)閉,就不能再寫數(shù)據(jù) |
import java
.io
.*
public class test3 {public static void main(String
[] args
) throws IOException
{OutputStreamWriter osw
= new OutputStreamWriter(new FileOutputStream("src\\osw.txt"));osw
.write(97);char[] chs
= {'a', 'b', 'c', 'd', 'e'};osw
.write(chs
);osw
.write(chs
, 0, chs
.length
);osw
.write(chs
, 1, 3);osw
.write("abcde");osw
.write("abcde", 0, "abcde".length());osw
.write("abcde", 1, 3);osw
.close();}
}
2.6字符流讀數(shù)據(jù)的2種方式
方法名說(shuō)明
| int read() | 一次讀一個(gè)字符數(shù)據(jù) |
| int read(char[] cbuf) | 一次讀一個(gè)字符數(shù)組數(shù)據(jù) |
import java
.io
.FileInputStream
;
import java
.io
.IOException
;
import java
.io
.InputStreamReader
;public class test4 {public static void main(String
[] args
) throws IOException
{InputStreamReader isr
= new InputStreamReader(new FileInputStream("src\\day19\\test3.java"));char[] chs
= new char[1024];int len
;while ((len
= isr
.read(chs
)) != -1) {System
.out
.print(new String(chs
, 0, len
));}isr
.close();}
2.7字符流復(fù)制Java文件
- 案例需求
把模塊目錄下的“ConversionStreamDemo.java” 復(fù)制到模塊目錄下的“Copy.java” - 實(shí)現(xiàn)步驟
根據(jù)數(shù)據(jù)源創(chuàng)建字符輸入流對(duì)象
根據(jù)目的地創(chuàng)建字符輸出流對(duì)象
讀寫數(shù)據(jù),復(fù)制文件
釋放資源 - 代碼實(shí)現(xiàn)
import java
.io
.*
;public class test5 {public static void main(String
[] args
) throws IOException
{InputStreamReader isr
= new InputStreamReader(new FileInputStream("src\\day19\\test3.java"));OutputStreamWriter osw
= new OutputStreamWriter(new FileOutputStream("src\\Copy.java"));char[] chs
= new char[1024];int len
;while ((len
= isr
.read(chs
)) != -1) {osw
.write(chs
,0,len
);}osw
.close();isr
.close();}
}
2.8字符流復(fù)制Java文件改進(jìn)版
- 案例需求
使用便捷流對(duì)象,把模塊目錄下的“ConversionStreamDemo.java” 復(fù)制到模塊目錄下的“Copy.java” - 實(shí)現(xiàn)步驟
根據(jù)數(shù)據(jù)源創(chuàng)建字符輸入流對(duì)象
根據(jù)目的地創(chuàng)建字符輸出流對(duì)象
讀寫數(shù)據(jù),復(fù)制文件
釋放資源 - 代碼實(shí)現(xiàn)
import java
.io
.*
;public class test6 {public static void main(String
[] args
) throws IOException
{FileReader fr
= new FileReader("src\\day19\\test3.java");FileWriter fw
= new FileWriter("src\\Copy.java");char[] chs
= new char[1024];int len
;while ((len
=fr
.read(chs
))!=-1) {fw
.write(chs
,0,len
);}fw
.close();fr
.close();}
}
2.9字符緩沖流
- 字符緩沖流介紹
BufferedWriter:將文本寫入字符輸出流,緩沖字符,以提供單個(gè)字符,數(shù)組和字符串的高效寫入,可
以指定緩沖區(qū)大小,或者可以接受默認(rèn)大小。默認(rèn)值足夠大,可用于大多數(shù)用途
BufferedReader:從字符輸入流讀取文本,緩沖字符,以提供字符,數(shù)組和行的高效讀取,可以指定緩
沖區(qū)大小,或者可以使用默認(rèn)大小。 默認(rèn)值足夠大,可用于大多數(shù)用途 - 構(gòu)造方法
方法名說(shuō)明
| BufferedWriter(Writer out) | 創(chuàng)建字符緩沖輸出流對(duì)象 |
| BufferedReader(Reader in) | 創(chuàng)建字符緩沖輸入流對(duì)象 |
import java
.io
.*
;public class test7 {public static void main(String
[] args
) throws IOException
{BufferedWriter bw
= new BufferedWriter(new FileWriter("src\\bw.txt"));bw
.write("hello\r\n");bw
.write("world\r\n");bw
.close();BufferedReader br
= new BufferedReader(new FileReader("src\\bw.txt"));char[] chs
= new char[1024];int len
;while ((len
=br
.read(chs
))!=-1) {System
.out
.print(new String(chs
,0,len
));}br
.close();}
}
2.10字符緩沖流復(fù)制Java文件
- 案例需求
把模塊目錄下的ConversionStreamDemo.java 復(fù)制到模塊目錄下的 Copy.java - 實(shí)現(xiàn)步驟
根據(jù)數(shù)據(jù)源創(chuàng)建字符緩沖輸入流對(duì)象
根據(jù)目的地創(chuàng)建字符緩沖輸出流對(duì)象
讀寫數(shù)據(jù),復(fù)制文件,使用字符緩沖流特有功能實(shí)現(xiàn)
釋放資源 - 代碼實(shí)現(xiàn)
import java
.io
.*
;
public class test8 {public static void main(String
[] args
) throws IOException
{BufferedReader br
= new BufferedReader(new FileReader("src\\day19\\test3.java"));BufferedWriter bw
= new BufferedWriter(new FileWriter("src\\Copy.txt"));char[] chs
= new char[1024];int len
;while ((len
=br
.read(chs
))!=-1) {bw
.write(chs
,0,len
);}bw
.close();br
.close();}
}
2.11字符緩沖流特有功能
BufferedWriter:
方法名說(shuō)明
| void newLine() | 寫一行行分隔符,行分隔符字符串由系統(tǒng)屬性定義 |
BufferedReader:
方法名說(shuō)明
| String readLine() | 讀一行文字。 結(jié)果包含行的內(nèi)容的字符串,不包括任何行終止字符如果流的結(jié)尾已經(jīng)到達(dá),則為null |
import java
.io
.*
;public class test9{public static void main(String
[] args
) throws IOException
{BufferedWriter bw
= new BufferedWriter(new FileWriter("src\\bw.txt"));for (int i
= 0; i
< 10; i
++) {bw
.write("hello" + i
);bw
.newLine();bw
.flush();}bw
.close();BufferedReader br
= new BufferedReader(new FileReader("src\\bw.txt"));String line
;while ((line
=br
.readLine())!=null
) {System
.out
.println(line
);}br
.close();}
}
2.12字符緩沖流特有功能復(fù)制Java文件
- 案例需求
使用特有功能把模塊目錄下的ConversionStreamDemo.java 復(fù)制到模塊目錄下的 Copy.java - 實(shí)現(xiàn)步驟
根據(jù)數(shù)據(jù)源創(chuàng)建字符緩沖輸入流對(duì)象
根據(jù)目的地創(chuàng)建字符緩沖輸出流對(duì)象
讀寫數(shù)據(jù),復(fù)制文件,使用字符緩沖流特有功能實(shí)現(xiàn)
釋放資源 - 代碼實(shí)現(xiàn)
import java
.io
.*
;public class test10 {public static void main(String
[] args
) throws IOException
{BufferedWriter bw
= new BufferedWriter(new FileWriter("src\\day19\\test1"));BufferedReader br
= new BufferedReader(new FileReader("src\\Copy.txt"));String line
;while ((line
=br
.readLine())!=null
) {bw
.write(line
); bw
.newLine(); bw
.flush();}bw
.close();br
.close();}
}
2.13IO流小結(jié)
3練習(xí)案例
3.1集合到文件
- 案例需求
把文本文件中的數(shù)據(jù)讀取到集合中,并遍歷集合。要求:文件中每一行數(shù)據(jù)是一個(gè)集合元素 - 實(shí)現(xiàn)步驟
創(chuàng)建字符緩沖輸入流對(duì)象
創(chuàng)建ArrayList集合對(duì)象
調(diào)用字符緩沖輸入流對(duì)象的方法讀數(shù)據(jù)
把讀取到的字符串?dāng)?shù)據(jù)存儲(chǔ)到集合中
釋放資源
遍歷集合 - 代碼實(shí)現(xiàn)
import java
.io
.*
;
import java
.util
.ArrayList
;public class test11 {public static void main(String
[] args
) throws IOException
{BufferedReader br
= new BufferedReader(new FileReader("src\\Copy.txt"));ArrayList
<String> array
= new ArrayList<String>();String line
;while ((line
= br
.readLine()) != null
) {array
.add(line
);}br
.close();for (String s
: array
) {System
.out
.println(s
);}}
}
3.2文件到集合
- 案例需求
把ArrayList集合中的字符串?dāng)?shù)據(jù)寫入到文本文件。要求:每一個(gè)字符串元素作為文件中的一行數(shù)據(jù) - 實(shí)現(xiàn)步驟
創(chuàng)建ArrayList集合
往集合中存儲(chǔ)字符串元素
創(chuàng)建字符緩沖輸出流對(duì)象
遍歷集合,得到每一個(gè)字符串?dāng)?shù)據(jù)
調(diào)用字符緩沖輸出流對(duì)象的方法寫數(shù)據(jù)
釋放資源 - 代碼實(shí)現(xiàn)
import java
.io
.BufferedWriter
;
import java
.io
.FileWriter
;
import java
.io
.IOException
;
import java
.util
.ArrayList
;public class test12 {public static void main(String
[] args
) throws IOException
{ArrayList
<String> array
= new ArrayList<String>();array
.add("hello");array
.add("world");array
.add("java");BufferedWriter bw
= new BufferedWriter(new FileWriter("src\\array.txt"));for (String s
: array
) {bw
.newLine();bw
.flush();}bw
.close();}
}
3.3點(diǎn)名器
- 案例需求
我有一個(gè)文件里面存儲(chǔ)了班級(jí)同學(xué)的姓名,每一個(gè)姓名占一行,要求通過程序?qū)崿F(xiàn)隨點(diǎn)名器 - 實(shí)現(xiàn)步驟
創(chuàng)建字符緩沖輸入流對(duì)象
創(chuàng)建ArrayList集合對(duì)象
調(diào)用字符緩沖輸入流對(duì)象的方法讀數(shù)據(jù)
把讀取到的字符串?dāng)?shù)據(jù)存儲(chǔ)到集合中
釋放資源
使用Random產(chǎn)生一個(gè)隨機(jī)數(shù),隨機(jī)數(shù)的范圍在:[0,集合的長(zhǎng)度)
把第6步產(chǎn)生的隨機(jī)數(shù)作為索引到ArrayList集合中獲取值
把第7步得到的數(shù)據(jù)輸出在控制臺(tái) - 代碼實(shí)現(xiàn)
import java
.io
.*
;
import java
.util
.*
;public class test13 {public static void main(String
[] args
) throws IOException
{BufferedReader br
= new BufferedReader(new FileReader("src\\bw.txt"));ArrayList
<String> array
= new ArrayList<String>();String line
;while ((line
= br
.readLine()) != null
) {array
.add(line
);}br
.close();Random r
= new Random(); int index
= r
.nextInt(array
.size());String name
= array
.get(index
);System
.out
.println("幸運(yùn)者是:" + name
);}
}
3.4集合到文件改進(jìn)版
- 案例需求
把ArrayList集合中的學(xué)生數(shù)據(jù)寫入到文本文件。要求:每一個(gè)學(xué)生對(duì)象的數(shù)據(jù)作為文件中的一行數(shù)據(jù) 格式:
學(xué)號(hào),姓名,年齡,居住地 舉例:itheima001,林青霞,30,西安 - 實(shí)現(xiàn)步驟
定義學(xué)生類
創(chuàng)建ArrayList集合
創(chuàng)建學(xué)生對(duì)象
把學(xué)生對(duì)象添加到集合中
創(chuàng)建字符緩沖輸出流對(duì)象
遍歷集合,得到每一個(gè)學(xué)生對(duì)象
把學(xué)生對(duì)象的數(shù)據(jù)拼接成指定格式的字符串
調(diào)用字符緩沖輸出流對(duì)象的方法寫數(shù)據(jù)
釋放資源 - 代碼實(shí)現(xiàn)
在這里插入代碼片
3.5文件到集合改進(jìn)版
- 案例需求
把文本文件中的數(shù)據(jù)讀取到集合中,并遍歷集合。要求:文件中每一行數(shù)據(jù)是一個(gè)學(xué)生對(duì)象的成員變量值 舉
例:itheima001,林青霞,30,西安 - 實(shí)現(xiàn)步驟
定義學(xué)生類
創(chuàng)建字符緩沖輸入流對(duì)象
創(chuàng)建ArrayList集合對(duì)象
調(diào)用字符緩沖輸入流對(duì)象的方法讀數(shù)據(jù)
把讀取到的字符串?dāng)?shù)據(jù)用split()進(jìn)行分割,得到一個(gè)字符串?dāng)?shù)組
創(chuàng)建學(xué)生對(duì)象
把字符串?dāng)?shù)組中的每一個(gè)元素取出來(lái)對(duì)應(yīng)的賦值給學(xué)生對(duì)象的成員變量值
把學(xué)生對(duì)象添加到集合
釋放資源
遍歷集合 - 代碼實(shí)現(xiàn)
學(xué)生類
同上
import java
.io
.BufferedReader
;
import java
.io
.FileReader
;
import java
.io
.IOException
;
import java
.util
.ArrayList
;public class test15 {public static void main(String
[] args
) throws IOException
{BufferedReader br
= new BufferedReader(new FileReader("src\\students.txt"));ArrayList
<Student> array
= new ArrayList<Student>();String line
;while ((line
= br
.readLine()) != null
) {String
[] strArray
= line
.split(",");Student s
= new Student();s
.setSid(strArray
[0]);s
.setName(strArray
[1]);s
.setAge(Integer
.parseInt(strArray
[2]));s
.setAddress(strArray
[3]);array
.add(s
);}br
.close();for (Student s
: array
) {System
.out
.println(s
.getSid() + "," + s
.getName() + "," + s
.getAge() + "," + s
.getAddress());}}
}
總結(jié)
以上是生活随笔為你收集整理的Java基础day18的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。