Java中常用的字節(jié)流和字符流

IO流(輸入流、輸出流)

字節(jié)流、字符流

  1.字節(jié)流:

InputStream、OutputStream

InputStream抽象了應(yīng)用程序讀取數(shù)據(jù)的方式;

OutputStream抽象了應(yīng)用程序?qū)懗鰯?shù)據(jù)的方式;

  2.EOF=End 讀到-1就讀到結(jié)尾

  3.輸入流的基本方法:

int b=in.read();讀取一個(gè)字節(jié)無(wú)符號(hào)填充到int低八位;-1是EOF;

in.read(byte[] buf)

in.read(byte[] buf,int start,int size)

  4.輸出流基本方法:

out.write(int b) 寫出一個(gè)byte到流,b的低8位;

out.write(byte[] buf) 將buf字節(jié)數(shù)組都寫入到流;

out.write(byte[] buf,int start,int size)

  5.FileInputStream--->具體實(shí)現(xiàn)了在文件上讀取數(shù)據(jù),下面請(qǐng)看最簡(jiǎn)單的文件讀?。?/p>

package com.wxd.test2;import java.io.FileInputStream;import java.io.IOException;publicclass IOUtil {

? ? /**? ? * 讀取指定文件內(nèi)容,按照16進(jìn)制輸出到控制臺(tái)

? ? * 并且每輸出10個(gè)byte換行

? ? * @param fileName

? ? */publicstaticvoidprintHex(String fileName)throws IOException {

? ? ? ? //把文件作為字節(jié)流進(jìn)行讀操作FileInputStream in =new FileInputStream(fileName);

? ? ? ? int b;

? ? ? ? inti=1;

? ? ? ? while((b=in.read())!=-1){

? ? ? ? ? ? if(b<=0xf){

? ? ? ? ? ? ? ? //單位數(shù)前面補(bǔ)0System.out.print(0);

? ? ? ? ? ? }

? ? ? ? ? ? System.out.print(Integer.toHexString(b)+" ");

? ? ? ? ? ? if(i++%10==0){

? ? ? ? ? ? ? ? System.out.println();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? in.close();

? ? }

}


  6.FileOutputStream實(shí)現(xiàn)了向文件中寫出byte數(shù)據(jù)的方法:


package com.wxd.test2;import java.io.FileOutputStream;import java.io.IOException;publicclass FileOutDemo1 {

? ? publicstaticvoidmain(String[] args)throws IOException{

? ? ? ? //如果該文件不存在則直接創(chuàng)建,如果存在刪除后創(chuàng)建FileOutputStream out=newFileOutputStream("demo/out.dat");

? ? ? ? out.write('A');//寫出了‘A’字符的低八位out.write('B');//寫出了‘B’字符的低八位inta=10;//write只能寫低八位寫一個(gè)int需要寫四次每次8位out.write(a>>>24);

? ? ? ? out.write(a>>>16);

? ? ? ? out.write(a>>>8);

? ? ? ? out.write(a);

? ? ? ? byte[] zg="中國(guó)".getBytes("UTF-8");

? ? ? ? out.write(zg);

? ? ? ? out.close();

? ? ? ? IOUtil.printHex("demo/out.dat");

? ? }

}

  7.DataOutputStream/DataInputStream對(duì)“流”功能的擴(kuò)展,可以更加方便的讀取int,long,字符等類型數(shù)據(jù):

writeInt()/writeDouble()/writeUTF()


package com.wxd.test2;import java.io.DataOutputStream;import java.io.FileOutputStream;import java.io.IOException;publicclass DosDemo {

? ? publicstaticvoidmain(String[] args)throws IOException {

? ? ? ? String file="demo/dos.dat";

? ? ? ? DataOutputStream dos=newDataOutputStream(new FileOutputStream(file));

? ? ? ? dos.writeInt(10);

? ? ? ? dos.writeInt(-10);

? ? ? ? dos.writeLong(10l);

? ? ? ? dos.writeDouble(10.5);

? ? ? ? //采用UTF-8編碼寫出dos.writeUTF("中國(guó)");

? ? ? ? //采用utf-16be編碼寫出dos.writeChars("中國(guó)");

? ? ? ? dos.close();

? ? }

}


package com.wxd.test2;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.IOException;publicclass DisDemo {

? ? publicstaticvoidmain(String[] args)throws IOException {

? ? ? ? String file="demo/dos.dat";

? ? ? ? DataInputStream dis=newDataInputStream(new FileInputStream(file));

? ? ? ? inti=dis.readInt();//其實(shí)做了4次read,因?yàn)閕nt是32位? ? ? ? System.out.println(i);

? ? ? ? i=dis.readInt();

? ? ? ? System.out.println(i);

? ? ? ? longl=dis.readLong();//其實(shí)做了8次read,應(yīng)為long是64位? ? ? ? System.out.println(l);

? ? ? ? doubled=dis.readDouble();

? ? ? ? System.out.println(d);

? ? ? ? String s=dis.readUTF();

? ? ? ? System.out.println(s);

? ? ? ? dis.close();

? ? }

}

7.BufferedInputStream&BufferedOutputStrean這兩個(gè)流為IO提供了帶緩沖區(qū)的操作,一般打開文件進(jìn)行寫入或讀取操作時(shí),都會(huì)加上緩沖,這種流模式提高了IO性能

從應(yīng)用程序中把數(shù)據(jù)放入文件,相當(dāng)于將一缸水倒入到另一個(gè)缸中:

FileOutputStream--->write()方法相當(dāng)于一滴一滴地把水轉(zhuǎn)移過去

DataOutputStream--->writeXxx()方法會(huì)方便一些,相當(dāng)于一瓢一瓢的把水轉(zhuǎn)移過去

BufferedOutputStream--->write()方法相當(dāng)于一瓢一瓢先放入桶中,再?gòu)耐爸械谷敫字?/p>

/***

? ? *進(jìn)行文件的拷貝,利用帶緩沖的字節(jié)流

? ? * @param srcFile

? ? * @param destFile

? ? * @throws IOException

? ? */publicstaticvoidcopyFileByBuffer(File srcFile,File destFile)throws IOException{

? ? ? ? if(!srcFile.exists()){

? ? ? ? ? ? thrownewIllegalArgumentException("文件:"+srcFile+"不存在");

? ? ? ? }

? ? ? ? if(!srcFile.isFile()){

? ? ? ? ? ? thrownewIllegalArgumentException(srcFile+"不是文件!");

? ? ? ? }

? ? ? ? BufferedInputStream bis=newBufferedInputStream(new FileInputStream(srcFile));

? ? ? ? BufferedOutputStream bos=newBufferedOutputStream(new FileOutputStream(destFile));

? ? ? ? int c;

? ? ? ? while((c=bis.read())!=-1){

? ? ? ? ? ? bos.write(c);

? ? ? ? ? ? bos.flush();//刷新緩沖區(qū),否則寫入不到;? ? ? ? }

? ? ? ? bis.close();

? ? ? ? bos.close();

? ? }

?字符流

1.編碼問題

2.認(rèn)識(shí)文本和文本文件

3.Java的文本(char)是16位無(wú)符號(hào)的整數(shù),是字符的unicode編碼(雙字節(jié)編碼)文件是byte?byte?byte...的數(shù)據(jù)序列

  文本文件是文本(char)序列按照某種編碼方案(utf-8,utf-16be,gbk)序列化為byte的存儲(chǔ)

4.字符流(Reader Writer):--->操作的是文本文件

  字符的處理,一次處理一個(gè)字符

  字符的底層任然是基本的字節(jié)序列

  字符流的基本實(shí)現(xiàn)

    InputStreamReader 完成byte流解析為char流,按照編碼解析

    OutputStreamWriter 提供char流到byte流,按照編碼處理


package com.wxd.test2;importjava.io.*;publicclass IsrAndOswDemo {

? ? publicstaticvoidmain(String[] args)throws IOException{

? ? ? ? //InputStreamReader被稱為橋梁流FileInputStream in=newFileInputStream("demo\\test.txt");//默認(rèn)項(xiàng)目的編碼,操作的時(shí)候要寫文件本身的編碼InputStreamReader isr=newInputStreamReader(in,"utf-8");

? ? ? ? FileOutputStream out=newFileOutputStream("demo\\test2.txt");

? ? ? ? OutputStreamWriter osw=newOutputStreamWriter(out,"utf-8");//? ? ? ? int c;//? ? ? ? while ((c=isr.read())!=-1){//? ? ? ? ? ? System.out.print((char)c);//? ? ? ? }char[] buffer=newchar[8*1024];

? ? ? ? int c;

? ? ? ? //批量讀取,放入buffer這個(gè)字符數(shù)組,從第0個(gè)位置開始放置,最多放buffe.length個(gè)

? ? ? ? //返回的是讀到的字符的個(gè)數(shù)while((c=isr.read(buffer,0,buffer.length))!=-1){

? ? ? ? ? ? String s=newString(buffer,0,c);

? ? ? ? ? ? System.out.println(s);

? ? ? ? ? ? osw.write(buffer,0,c);

? ? ? ? ? ? osw.flush();

? ? ? ? }

? ? ? ? isr.close();

? ? ? ? osw.close();

? ? }

}


5.FileReader/FileWriter(這種方式bu)

package com.wxd.test2;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;publicclass FrAndFwDemo {

? ? publicstaticvoidmain(String[] args)throws IOException{

? ? ? ? FileReader fr=newFileReader("demo\\test.txt");

? ? ? ? FileWriter fw=newFileWriter("demo\\text2.txt",true);//設(shè)置為true在后面追加char[] buffer=newchar[2056];

? ? ? ? int c;

? ? ? ? while((c=fr.read(buffer,0,buffer.length))!=-1){

? ? ? ? ? ? fw.write(buffer,0,c);

? ? ? ? ? ? fw.flush();

? ? ? ? }

? ? ? ? fr.close();

? ? ? ? fw.close();

? ? }

}

6.字符流的過濾器

  BufferedReader ----->readLine一次讀一行

  BufferedWriter/PrintWriter ---->寫一行

package com.wxd.test2;importjava.io.*;publicclass BrAndBwOrPwDemo {

? ? publicstaticvoidmain(String[] args)throws IOException {

? ? ? ? //對(duì)文件進(jìn)行讀寫操作BufferedReader br =newBufferedReader(newInputStreamReader(newFileInputStream("demo\\test.txt")));

? ? ? ? /*BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo\\test3.txt")));*/? ? ? ? PrintWriter pw =newPrintWriter("demo\\test3.txt");

? ? ? ? String line;

? ? ? ? while((line = br.readLine()) !=null) {

? ? ? ? ? ? System.out.println(line);//一次讀一行,并不能識(shí)別換行/*bw.write(line);

? ? ? ? ? ? //單獨(dú)寫出換行操作

? ? ? ? ? ? bw.newLine();

? ? ? ? ? ? bw.flush();*/? ? ? ? ? ? pw.println(line);

? ? ? ? ? ? pw.flush();

? ? ? ? }

? ? ? ? br.close();//? ? ? ? bw.close();? ? ? ? pw.close();

? ? } ? ? ? ? ?歡迎工作一到五年的Java工程師朋友們加入Java群:?891219277

群內(nèi)提供免費(fèi)的Java架構(gòu)學(xué)習(xí)資料(里面有高可用、高并發(fā)、高性能及分布式、Jvm性能調(diào)優(yōu)、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個(gè)知識(shí)點(diǎn)的架構(gòu)資料)合理利用自己每一分每一秒的時(shí)間來學(xué)習(xí)提升自己,不要再用"沒有時(shí)間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個(gè)交代!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容