java IO流:字节流、字符流
一、流的分類
? ?1.操作數(shù)據(jù)的單位:字節(jié)流、字符流
? ?2.數(shù)據(jù)的流向:輸入流、輸出流
? ?3.流的角色:節(jié)點(diǎn)流、處理流
二、流的體系結(jié)構(gòu)
| 抽象基類 | 節(jié)點(diǎn)流(文件流) | 緩沖流(屬于處理流) |
| InputStream | FileInputStream | BufferedInputStream |
| OutputStream | FileOutputStream | BufferedOutputStream |
| Reader | FileReader | BufferedReader |
| Writer | FileWriter | BufferedWriter |
三、使用
?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的長(zhǎng)度會(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)來(lái)設(shè)置為以下操作是對(duì)原來(lái)的文本進(jìn)行追加。為false時(shí),則為覆蓋原來(lái)的文件并且重新寫入。
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èn)題。
- 上一篇: js 位运算符 ~, ,| ,^
- 下一篇: sql 把特定数据排在最前面