日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java IO流:字节流、字符流

發(fā)布時(shí)間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java IO流:字节流、字符流 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、流的分類

? ?1.操作數(shù)據(jù)的單位:字節(jié)流、字符流

? ?2.數(shù)據(jù)的流向:輸入流、輸出流

? ?3.流的角色:節(jié)點(diǎn)流、處理流

二、流的體系結(jié)構(gòu)

抽象基類節(jié)點(diǎn)流(文件流)緩沖流(屬于處理流)
InputStreamFileInputStreamBufferedInputStream
OutputStreamFileOutputStreamBufferedOutputStream
ReaderFileReaderBufferedReader
WriterFileWriterBufferedWriter

三、使用

?1.FileReader和FileWriter的使用

讀入:

//實(shí)例化File類的對(duì)象,指明要操作的文件FileReader fr = null;try {File file = new File("hello.txt");//將文件中的數(shù)據(jù)讀入程序(內(nèi)存)中//提供具體的流fr = new FileReader(file);//讀入數(shù)據(jù)//read: 返回讀入的字符 * @return The character read, or -1 if the end of the stream has been reached:返回-1時(shí)讀完int data = fr.read();while(data!= -1){System.out.println((char) data);data = fr.read();}//關(guān)閉流} catch (IOException e) {e.printStackTrace();}finally {try {fr.close(); //一定要執(zhí)行關(guān)閉操作,放在finally} catch (IOException e) {e.printStackTrace();}}

注:close()方法一定要放在finally里,避免出現(xiàn)異常不再執(zhí)行finally中的關(guān)閉流操作,導(dǎo)致錯(cuò)誤。

設(shè)置每次讀取n個(gè)字節(jié),遍歷讀取:

//一次讀n個(gè)字符,使用read(cbuf)方法去讀。 char[] cbuf = new char[3]; int len;//讀到的數(shù)組里的元素個(gè)數(shù) while ((len = fr.read(cbuf)) != -1) {//每次直接遍歷取出cbuf的長度會(huì)導(dǎo)致重復(fù)讀,應(yīng)該遍歷已經(jīng)讀到的數(shù)據(jù),讀到幾個(gè)遍歷幾個(gè)for (int i = 0; i < len.length; i++) {System.out.println(cbuf[i]);} }

寫出:

? ? ? 寫出時(shí),對(duì)應(yīng)的file是可以不存在的,在寫出時(shí)會(huì)自動(dòng)創(chuàng)建文件。如果file已存在,可以使用 new FileWriter(file,true)來設(shè)置為以下操作是對(duì)原來的文本進(jìn)行追加。為false時(shí),則為覆蓋原來的文件并且重新寫入。

try {File file = new File("hello1.txt");//提供FileWriter對(duì)象,用于數(shù)據(jù)的寫出fw = new FileWriter(file);//寫出fw.write("happy hello"); } catch (IOException e) {e.printStackTrace(); }finally {try {fw.close();} }

寫入+寫出操作:

try {//創(chuàng)建file對(duì)象,指明讀入寫出的文件File dufile = new File("hello.txt");File xieFile = new File("hello2.txt");//創(chuàng)建輸入流和輸出流的對(duì)象fr = new FileReader(dufile);fw = new FileWriter(xieFile);//讀入、寫出數(shù)據(jù)char[] cbuf = new char[5];int len;//每次讀到的字符的個(gè)數(shù)while( (len = fr.read(cbuf))!=-1){ //每次寫入Len個(gè)字符fw.write(cbuf,0,len);} }

2.FileInputStream和FileOutputStream

? ??對(duì)于文本文件,使用字符流進(jìn)行處理;對(duì)于非文本文件,使用字節(jié)流處理,字節(jié)流同時(shí)可對(duì)文本文件進(jìn)行直接復(fù)制操作。

使用字節(jié)流FileInputStream處理非文本文件:

FileInputStream fis = null;FileOutputStream fos = null;try {File srcFile = new File("1.jpg");File destFile = new File("2.jpg");fis = new FileInputStream(srcFile);fos = new FileOutputStream(destFile);//字節(jié)流,操作字節(jié)byte[] buffer = new byte[5];int len;while( (len = fis.read(buffer))!=-1){fos.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();}finally {if(fos != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}if (fis!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}

可以據(jù)此寫一個(gè)指定文件的復(fù)制,改成copyFile方法

可以看一下花費(fèi)的時(shí)間,后面可與更高效率的緩沖流進(jìn)行對(duì)比:

long start = System.currentTimeMillis(); copyFile("1.jpg","3.jpg"); long end = System.currentTimeMillis(); System.out.println("操作花費(fèi)的時(shí)間為:"+(end - start)+"ms");

先寫到這……后面再寫

總結(jié)

以上是生活随笔為你收集整理的java IO流:字节流、字符流的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。