字符流
字符流產(chǎn)生的原因:
1.每次只能夠讀取一個(gè)字節(jié)或者一個(gè)字節(jié)數(shù)組,每次在需要轉(zhuǎn)換成字符或者字符串的時(shí)候不是很方便
2.不同的操作系統(tǒng)針對(duì)換行符的處理不方便
3.有的時(shí)候會(huì)出現(xiàn)中文亂碼(中文占兩個(gè)字節(jié),如果針對(duì)中文中某個(gè)字節(jié)做了轉(zhuǎn)換或者顯示,就會(huì)出現(xiàn)亂碼)
4.如果需要讀取某一行數(shù)據(jù),非常不方便
5.字節(jié)流雖然作為萬(wàn)能流,但是在對(duì)字符進(jìn)行處理的時(shí)候不是很方便,可能因?yàn)槟承┤藶榈牟僮鞒霈F(xiàn)亂碼現(xiàn)象,所以Java就提供了字符流
Java就設(shè)計(jì)了字符流
字符流就是一次性讀取一個(gè)字符或者一個(gè)字符數(shù)組(字符串)
字節(jié)流和字符流的選取
1.如果字節(jié)流不方便處理 字符/字符數(shù)組/字符串等文件,可以考慮使用字符流
2.針對(duì)二進(jìn)制文件,使用記事本打開(kāi)看不懂的文件考慮字節(jié)流,使用記事本打開(kāi)能夠看懂使用字符流
代碼演示使用FileWriter和FileReader復(fù)制文件內(nèi)容
構(gòu)造方法類似FileInputStream和FileOutputStream成員方法完全繼承自父類OutputStreamWriter和InputStreamReader
publicclassCharIODemo{
? ? publicstaticvoidmain(String[]args) {
? ? ? ? copyFile2("fr.txt","fw2.txt");
? ? }
?
? ? publicstaticvoidcopyFile(StringsrcFileName,StringdescFileName) {
? ? ? ? try(FileReaderfr=newFileReader(srcFileName);
? ? ? ? ? ? ? ? FileWriterfw=newFileWriter(descFileName);) {
? ? ? ? ? ? // 一次性讀取一個(gè)字符
? ? ? ? ? ? intch=0;
? ? ? ? ? ? while((ch=fr.read())!=-1) {
? ? ? ? ? ? ? ? fw.write(ch);
? ? ? ? ? ? ? ? fw.flush();
? ? ? ? ? ? }
? ? ? ? }catch(Exceptione) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
?
? ? }
? ? publicstaticvoidcopyFile2(StringsrcFileName,StringdescFileName) {
? ? ? ? try(FileReaderfr=newFileReader(srcFileName);
? ? ? ? ? ? ? ? FileWriterfw=newFileWriter(descFileName);) {
? ? ? ? ? ? // 一次性讀取一個(gè)字符數(shù)組
? ? ? ? ? ? intlen=0;
? ? ? ? ? ? char[]chs=newchar[1024];
? ? ? ? ? ? while((len=fr.read(chs))!=-1) {
? ? ? ? ? ? ? ? fw.write(chs,0,len);
? ? ? ? ? ? ? ? fw.flush();
? ? ? ? ? ? }
? ? ? ? }catch(Exceptione) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
?
? ? }
}
?
轉(zhuǎn)換流
OutputStreamWriter(轉(zhuǎn)換流,是字節(jié)流通向字符流的橋梁,可使用指定的 charset 將要寫(xiě)入流中的字符編碼成字節(jié)。它使用的字符集可以由名稱指定或顯式給定,否則將接受平臺(tái)默認(rèn)字符集編碼。)轉(zhuǎn)換流的核心功能在于將字節(jié)流轉(zhuǎn)換成字符流,這樣就能夠使用更多好的方法來(lái)處理文本文件
構(gòu)造方法
1.OutputStreamWriter(OutputStream out)? 創(chuàng)建使用默認(rèn)字符編碼的字符輸出轉(zhuǎn)換流2.OutputStreamWriter(OutputStream out, Charset cs)? 創(chuàng)建使用cs字符集的字符輸出轉(zhuǎn)換流3.OutputStreamWriter(OutputStream out, CharsetEncoder enc)? 創(chuàng)建使用enc字符集編碼器的4.OutputStreamWriter(OutputStream out, String charsetName)? 創(chuàng)建使用指定字符集的? ?
轉(zhuǎn)換流的編碼問(wèn)題:
OutputStreamWriter(OutputStream out, Charset cs)? 創(chuàng)建使用cs字符集的字符輸出轉(zhuǎn)換流OutputStreamWriter(OutputStream out, CharsetEncoder enc)? 創(chuàng)建使用enc字符集編碼器的OutputStreamWriter(OutputStream out, String charsetName)? 創(chuàng)建使用指定字符集的
publicclassIODemo{
? ? publicstaticvoidmain(String[]args)throwsException{
? ? ? ? OutputStreamWriterosw=newOutputStreamWriter(newFileOutputStream("osw2.txt"),"UTF-8");
? ? ? ? osw.write("床前明月光\r\n");
? ? ? ? osw.write("疑似地上霜\r\n");
? ? ? ? osw.write("舉頭忘明月\r\n");
? ? ? ? osw.write("低頭思故鄉(xiāng)");
? ? ? ? osw.flush();
? ? ? ? osw.close();
? ? ? ? InputStreamReaderisr=newInputStreamReader(newFileInputStream("osw2.txt"),"gbk");
? ? ? ? intch=0;
? ? ? ? while((ch=isr.read())!=-1) {
? ? ? ? ? ? System.out.print((char)ch);
? ? ? ? }
? ? ? ? isr.close();
? ? }
}
?
高效字符緩沖流
類似于字節(jié)流的BufferedOutputStream和BufferedInputStream,字符流同樣存在字符緩沖流同理,字符緩沖流也是為了提高字符流的效率。BufferedWriter和BufferedReader繼承自Writer和Reader,所以具備原有的讀取方法,但是還存在自己特有的方法。特有的方法:BufferedWriter:void newLine()BufferedReader:String readLine()
代碼演示字符緩沖流拷貝文件內(nèi)容
publicclassCharIODemo{
? ? publicstaticvoidmain(String[]args)throwsException{
? ? ? ? // write();
? ? ? ? // read();
? ? ? ? copyFile("ByteIODemo01.java","good.txt");
? ? }
?
? ? publicstaticvoidcopyFile(StringsrcFileName,StringdescFileName) {
? ? ? ? try(BufferedReaderbr=newBufferedReader(newFileReader(srcFileName));
? ? ? ? ? ? ? ? BufferedWriterbw=newBufferedWriter(newFileWriter(descFileName))) {
?
? ? ? ? ? ? // 一次性讀取一行
? ? ? ? ? ? Stringline=null;
? ? ? ? ? ? while((line=br.readLine())!=null) {
? ? ? ? ? ? ? ? bw.write(line);
? ? ? ? ? ? ? ? bw.newLine();
? ? ? ? ? ? ? ? bw.flush();
? ? ? ? ? ? }
? ? ? ? }catch(Exceptione) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
?
? ? }
?
? ? privatestaticvoidread()throwsException{
? ? ? ? BufferedReaderbr=newBufferedReader(newFileReader("br.txt"));
?
? ? ? ? Stringline=null;
? ? ? ? while((line=br.readLine())!=null) {
? ? ? ? ? ? System.out.println(line);
? ? ? ? }
?
? ? ? ? br.close();
? ? }
?
? ? privatestaticvoidwrite()throwsException{
? ? ? ? BufferedWriterbw=newBufferedWriter(newFileWriter("bw.txt"));
?
? ? ? ? bw.write("Hello");
? ? ? ? bw.newLine();
? ? ? ? bw.flush();
? ? ? ? bw.write("World");
? ? ? ? bw.newLine();
? ? ? ? bw.flush();
?
? ? ? ? bw.close();
? ? }
}