Java IO流的使用

下面部分內(nèi)容是參考Oubo的博客——Java IO流學(xué)習(xí)總結(jié)

Java流操作有關(guān)的類或者接口:
說(shuō)明
File 文件類
InputStream 字節(jié)輸入流
OutputStream 字節(jié)輸出流
Reader 字符輸入流
Writer 字符輸出流

IO流.jpg

字符流和字節(jié)流
  • 字符流的由來(lái):
    因?yàn)閿?shù)據(jù)編碼的不同,而有了對(duì)字符進(jìn)行高效操作的流對(duì)象。本質(zhì)其實(shí)就是基于字節(jié)流讀取時(shí),去查了指定的碼表(ASCII、Unicode、utf-8等等)。
  • 字節(jié)流和字符流的區(qū)別:
    讀寫(xiě)單位不同:字節(jié)流以字節(jié)(8bit)為單位;字符流以字符為單位,根據(jù)碼表映射字符,一次可能讀多個(gè)字節(jié)。
    處理對(duì)象不同:字節(jié)流能處理所有類型的數(shù)據(jù)(如圖片、avi等),而字符流只能處理字符類型的數(shù)據(jù)。
    結(jié)論:只要是處理純文本數(shù)據(jù),就優(yōu)先考慮使用字符流。 除此之外都使用字節(jié)流。
輸出流和輸入流
  • 輸入流只能進(jìn)行讀操作,輸出流只能進(jìn)行寫(xiě)操作?。?!

    以前老是將這個(gè)攪亂了,百度別人又說(shuō)一大段,這下好啦 一句話總結(jié)了~


字節(jié)流讀取文件的幾種方式:
  1. 使用FileInputStream,如果不設(shè)置byte[],就會(huì)很慢很慢,建議使用byte[],關(guān)鍵代碼:read(byte[])?!緯?huì)快很多很多】
  2. 使用BufferedInputSteram,這個(gè)有緩存區(qū)功能。盡可能的減少發(fā)送IO操作?!疚募〉臅r(shí)候可能方法①會(huì)比較快,不過(guò)綜合起來(lái)還是用Buffer會(huì)更好】
FileInputStream和BufferedInputStream的區(qū)別:
  • 沒(méi)有緩存區(qū),那么每read一次,就會(huì)發(fā)送一次IO操作;
  • 有緩存區(qū),第一次read時(shí),會(huì)一下讀取x個(gè)字節(jié)放入緩存區(qū),然后后續(xù)的read都會(huì)從緩存中讀取,當(dāng)read到緩存區(qū)末尾時(shí),會(huì)再次讀取x個(gè)字節(jié)放入緩存區(qū)。
  • 很明顯,第二種方式,會(huì)減少IO操作,效率更高,缺點(diǎn)就是,內(nèi)存占用的多。
基本概念都說(shuō)了,接下來(lái)開(kāi)始學(xué)點(diǎn)實(shí)際的~
1.使用FileInputSteam讀取文件內(nèi)容
File file = new File("E:\\haha.txt");
try{
    FileInputStream in = new FileInputStream(file);
    //字節(jié)數(shù)組輸出流,這里是用于存放一下獲得的文件內(nèi)容
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    int len = 0;
    //定義一個(gè)buffer
    //FileInputSteam的read()是一個(gè)字節(jié)一個(gè)字節(jié)的讀,如果使用read(byte[])效率就會(huì)快很多
    byte[] buffer = new byte[1024];
    while((len=in.read(buffer)) > 0){
        //將buffer的內(nèi)容寫(xiě)入ByteArrayOutputStream里面
        bout.write(buffer, 0, len);
   }
   //將ByteArrayOutputStream里的內(nèi)容以byte[]取出
   byte[] data = bout.toByteArray();
   for(byte b: data){
       System.out.print((char)b);
   }
   in.close();
   bout.close();
   buffer = null;
}catch(IOException e){
   e.printStackTrace();
}
2.使用BufferedInputSteam讀取文件內(nèi)容

(這里只貼出部分更改代碼)

FileInputStream in = new FileInputStream(file);
//帶有緩沖區(qū)的InputStream
BufferedInputStream bin = new BufferedInputStream(in);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len=bin.read(buffer)) > 0){
    bout.write(buffer, 0, len);
}
in.close();
bin.close();
bout.close();
buffer = null;
3.使用FileOutputSteam寫(xiě)文件

(這里只貼出部分更改代碼)

File fileIn = new File("E:\\haha.txt");
File fileOut = new File("E:\\xixi.txt");
try{
    FileInputStream in = new FileInputStream(fileIn);
    //FileOutputStream(File file, boolean append)
    //第二個(gè)參數(shù)如果是true就是在內(nèi)容的最后添加文字,false就替換原有的內(nèi)容。
    FileOutputStream out = new FileOutputStream(fileOut,true);
    int len = 0;
    byte[] buffer = new byte[1024];
    while((len=in.read(buffer)) > 0){
        out.write(buffer, 0, len);
    }
    in.close();
    out.close();
    buffer = null;
}catch(IOException e){
    e.printStackTrace();
}
4.使用BufferedOutputStream寫(xiě)文件

(這里只貼出部分更改代碼)

File fileIn = new File("E:\\haha.txt");
File fileOut = new File("E:\\xixi.txt");
try{
    FileInputStream in = new FileInputStream(fileIn);
    FileOutputStream out = new FileOutputStream(fileOut,false);
    BufferedInputStream bin = new BufferedInputStream(in);
    BufferedOutputStream bout = new BufferedOutputStream(out);
    int len = 0;
    byte[] buffer = new byte[1024];
    while((len=bin.read(buffer)) > 0){
        bout.write(buffer, 0, len);
    }
    in.close();
    //bout清除緩存,必須在out.close(),不然會(huì)報(bào)錯(cuò)
    bout.flush();
    out.close();
    bin.close();
    bout.close();
    buffer = null;
}catch(IOException e){
    e.printStackTrace();
}
5.再補(bǔ)充一點(diǎn)點(diǎn)字節(jié)流的知識(shí)
  • ByteArrayInputStream 字節(jié)數(shù)組輸入流
  • ByteArrayOutputStream 字節(jié)數(shù)組輸出流

字符流讀取文件:
1.字符流和字節(jié)流的轉(zhuǎn)換:
  • InputStreamReader 將字節(jié)流轉(zhuǎn)換成字符流
  • OutputStreamWriter 將字符流轉(zhuǎn)換成字節(jié)流

為什么呢???帶著疑問(wèn)往下看
InputStreamReader的構(gòu)造函數(shù)是InputStreamReader(InputStream in),BufferedReader的構(gòu)造函數(shù)是BufferedReader(Reader in),然而InputStreamReader就是繼承于Reader,所以就有了以下的關(guān)系。

FileInputSteam in = new FileInputStrem(file);
InputStreamReader inReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inReader);
2.使用字符流讀取文件:
FileReader re = new FileReader("E:\\haha.txt");
FileWriter wr = new FileWriter("E:\\xixi.txt",true);
BufferedReader reader = new BufferedReader(re);
BufferedWriter writer = new BufferedWriter(wr);
String str = null;
//readLine()是一行一行的讀取內(nèi)容,直到遇到換行符
while((str=reader.readLine()) != null){
    //這里是輸出流,如果沒(méi)有加上"\r\n",輸出的內(nèi)容就沒(méi)有換行  
    //試了一下只是"\n",發(fā)現(xiàn)也沒(méi)有換行的效果,一定是要"\r\n"。(不明覺(jué)厲哈哈)
    writer.write(str + "\r\n");
    System.out.println(str);
}
reader.close();
writer.flush();
writer.close();
str = null;

關(guān)于Java IO就知道這么多內(nèi)容了~~
好好學(xué)習(xí),天天向上。<( ̄oo, ̄)/


Potato_zero.jpg
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 1 IO(二)No19 【 緩沖流:內(nèi)置了緩沖區(qū),對(duì)現(xiàn)有的流對(duì)象進(jìn)行了封裝,實(shí)現(xiàn)了高效的讀寫(xiě)操作并增強(qiáng)了功能 ...
    征程_Journey閱讀 804評(píng)論 0 1
  • tags:io categories:總結(jié) date: 2017-03-28 22:49:50 不僅僅在JAVA領(lǐng)...
    行徑行閱讀 2,301評(píng)論 0 3
  • 概述: 1、IO流:即Input Output的縮寫(xiě)。 2、特點(diǎn):1)IO流用來(lái)處理設(shè)備間的數(shù)據(jù)傳輸。2)Java...
    玉圣閱讀 1,324評(píng)論 0 3
  • 一、IO流整體結(jié)構(gòu)圖 流是一組有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對(duì)數(shù)據(jù)傳輸?shù)目偡Q或抽象。即數(shù)據(jù)在兩設(shè)備間的傳輸稱...
    慕凌峰閱讀 1,528評(píng)論 0 12
  • 摘要 Java I/O是Java技術(shù)體系中非常基礎(chǔ)的部分,它是學(xué)習(xí)Java NIO的基礎(chǔ)。而深入理解Java NI...
    biakia閱讀 7,779評(píng)論 7 81

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