Java 的 IO 流
接著上一篇的 “Java 的 File 類” 的隨筆,在File類的基礎(chǔ)上,我們就走進(jìn)Java的IO流吧。
?
流的概念和作用
流是一組有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對數(shù)據(jù)傳輸?shù)目偡Q或抽象。即數(shù)據(jù)在兩設(shè)備間的傳輸稱為流,流的本質(zhì)是數(shù)據(jù)傳輸,根據(jù)數(shù)據(jù)傳輸特性將流抽象為各種類,方便更直觀的進(jìn)行數(shù)據(jù)操作。
?
流的分類:
(1)、按照數(shù)據(jù)流向的不同分為:輸入流(從磁盤、網(wǎng)絡(luò)等讀取到內(nèi)存,只能讀,不能寫)、輸出流(從內(nèi)存寫出到磁盤、網(wǎng)絡(luò),只能寫,不能讀)
(2)、按照處理數(shù)據(jù)的單位不同分為:字節(jié)流(以字節(jié)為基本操作單位)、字符流(以字符為基本操作單位)
(3)、按照角色的不同分為:節(jié)點(diǎn)流(向某個(gè)IO設(shè)備/節(jié)點(diǎn)(磁盤文件、網(wǎng)絡(luò)等)直接讀寫數(shù)據(jù),也稱為低級流)、處理流(用于包裝一個(gè)已存在的流,通過這個(gè)已存在的流來進(jìn)行讀寫操作,并不直接操作IO節(jié)點(diǎn),也稱為高級流、包裝流)
?
Java流類結(jié)構(gòu):
| 分類 | 字節(jié)輸入流 | 字節(jié)輸出流 | 字符輸入流 | 字符輸出流 |
| 抽象基類 | InputStream | OutputStream | Reader | Writer |
| 操作文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
| 操作數(shù)組 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
| 操作字符串 | ? | ? | StringReader | StringWriter |
| 緩沖流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
| 轉(zhuǎn)換流 | ? | ? | InputStreamReader | OutputStreamWriter |
| 對象流(用于序列化) | ObjectInputStream | ObjectOutputStream | ? | ? |
| 抽象基類(用于過濾) | FilterInputStream | FilterOutputStream | FilterReader | FilterWriter |
| 打印流(輸出功能極其大) | ? | PrintStream | ? | PrintWriter |
?
java的IO流的用法
InputStream是字節(jié)輸入流的頂級父類,常用方法:
- int? read()? ? ?//讀取一個(gè)字節(jié),返回該字節(jié)數(shù)據(jù)的Unicode碼值
- int? read(byte[]? buff)? ? //最多讀取buff.length個(gè)字節(jié),將讀取的數(shù)據(jù)放在buff數(shù)組中,返回實(shí)際讀取的字節(jié)數(shù)
- int? read(byte[]? buff, int off, int length)? ? //最多讀取length個(gè)字節(jié),放在buff數(shù)組中,從數(shù)組的off位置開始放置數(shù)據(jù),返回實(shí)際讀取的字節(jié)數(shù)。off一般設(shè)置為0,length一般設(shè)置為buff的長度。
?
?
?
OutputStream是字節(jié)輸出流的頂級父類,常用方法:
- void? write(int? i)? ? \\輸出一個(gè)字節(jié),i是碼值,即read()得到的碼值,輸出i對應(yīng)的字節(jié)
- void? write(byte[]? buff)? ?//輸出整個(gè)字節(jié)數(shù)組的內(nèi)容
- void? write(byte[]? buff, int? off, int? length)? ? //把字節(jié)數(shù)組從off位置開始,輸出長度為length字節(jié)的內(nèi)容
?
?
Reader時(shí)字符輸入流的頂級父類,常用方法:
- int? read()? ? ?//讀取一個(gè)字符,返回該字符的Unicode碼值,注意并不是返回該字符。
- int? read(char[]? buff)? ?//最多讀取buff.length個(gè)字符,放在buff數(shù)組中,返回實(shí)際讀取的字符數(shù)
- int? read(char[]? buff, int? off, int? length)?//最多讀取length個(gè)字節(jié),放在buff數(shù)組中,從數(shù)組的off位置開始放置數(shù)據(jù),返回實(shí)際讀取的字符數(shù)。off一般設(shè)置為0,length一般設(shè)置為buff的長度 File file = new File("1.txt");FileReader fr = null;try{fr = new FileReader(file);char[] c = new char[24];int len;while((len = fr.read(c)) != -1){for(int i=0;i<len;i++){System.out.print(c[i]);}}}catch(Exception e){e.printStackTrace();}finally{if(fr != null){try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}
?
?
?
?
Writer是字符輸出流的頂級父類,常用方法:
- void? write(int? i)? ? //輸出一個(gè)字符,i是碼值,輸出的是i對應(yīng)的字符
- void? write(char[]? buff)? ? //輸出整個(gè)char[]的內(nèi)容
- void? write(char[]? buff,? int? off,? int? length)? ? ? //把char[]從off位置開始,輸出長度為length字符的內(nèi)容
可以用String代替char[],所以Writer還具有以下2個(gè)方法:
- void? write(String str)
- void? write(String str, int off, int length)
?
?
緩沖流是和4級頂級父類對應(yīng)的:加前綴Buffered
InputStream? ?BufferedInputStream? ?字節(jié)輸入緩沖流,可作為所有字節(jié)輸入流類的緩沖流
OutputStream? ?BufferedOutputStream? ? 字節(jié)輸出緩沖流
Reader? ? ?BufferedReader? ? 字符輸入緩沖流
Writer BufferedWriter? ? 字符輸出緩沖流
//用 BufferedReader 把文件讀進(jìn)來, 再用 BufferedWriter 把內(nèi)容寫出去File file = new File("hello.txt");File file1 = new File("hello3.txt");FileReader fr = null;BufferedReader br = null;FileWriter fw = null;BufferedWriter bw = null;try{fr = new FileReader(file);fw = new FileWriter(file1);br = new BufferedReader(fr);bw = new BufferedWriter(fw);String str;while((str = br.readLine()) != null){ // System.out.println(str); bw.write(str);bw.newLine();bw.flush();}}catch(Exception e){e.printStackTrace();}finally{if(bw != null){try {bw.close();} catch (IOException e) {e.printStackTrace();}}if(br != null){try {br.close();} catch (IOException e) {e.printStackTrace();}}}}?
?
其他方法我就不一一說明了。有興趣的同學(xué)可以自己再深入研究。
最后我就提供文件復(fù)制的通用方法吧。自己寫的通用類。讓自己更好的去了解Java的IO流的使用。
package io; /** 實(shí)現(xiàn)文件的復(fù)制*/ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class CopyFile {// 字節(jié)文件的復(fù)制方法public static void copyFileByte(String src, String dest) {// 1、提供讀入、寫出的文件File file1 = new File(src);File file2 = new File(dest);// 2、提供相應(yīng)的流FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(file1);fos = new FileOutputStream(file2);// 3、實(shí)現(xiàn)文件的復(fù)制byte[] b = new byte[20];int len;while ((len = fis.read(b)) != -1) {fos.write(b, 0, len);}} catch (Exception e) {e.printStackTrace();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}/** 使用FileReader 、 FileWriter 可以實(shí)現(xiàn)文本文件的復(fù)制* 對于非文本文件(視頻文件、音頻文件、圖片),只能使用字節(jié)流復(fù)制。*/public static void copyFileChar(String src, String dest) {// 1、提供讀入、寫出的文件File file1 = new File(src);File file2 = new File(dest);// 2、提供相應(yīng)的流FileReader fr = null;FileWriter fw = null;try {fr = new FileReader(file1);fw = new FileWriter(file2);// 3、實(shí)現(xiàn)文件的復(fù)制char[] c = new char[24];int len;while ((len = fr.read(c)) != -1) {fw.write(c, 0, len);}} catch (Exception e) {e.printStackTrace();} finally {if (fw != null) {try {fw.close();} catch (IOException e) {e.printStackTrace();}}if (fr != null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}//使用BufferedInputStream和BufferedOutputStream實(shí)現(xiàn)非文本文件的復(fù)制public static void copyFileBuffered1(String src, String dest) {//1、提供讀入、寫出的文件File file1 = new File(src);File file2 = new File(dest);//2、先創(chuàng)建相應(yīng)的節(jié)點(diǎn)流:FileInputStream、 FileOutputStreamFileInputStream fis = null;FileOutputStream fos = null;//3、再創(chuàng)建緩沖流:BufferedInputStream 、 BufferedOutputStreamBufferedInputStream bis = null;BufferedOutputStream bos = null;try {fis = new FileInputStream(file1);fos = new FileOutputStream(file2);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);//4、具體實(shí)現(xiàn)文件復(fù)制的操作byte[] b = new byte[1024];int len;while((len = bis.read(b)) != -1){bos.write(b, 0, len);bos.flush();}}catch (Exception e) {e.printStackTrace();} finally {if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}}} }?
現(xiàn)在自己也準(zhǔn)備大四了,所以自己要捉緊時(shí)間去學(xué)習(xí),博客里面有什么問題的可以跟我說一下,大家一起交流一下學(xué)習(xí)的經(jīng)驗(yàn)。
?
轉(zhuǎn)載于:https://www.cnblogs.com/HHHY/p/10964567.html
總結(jié)
以上是生活随笔為你收集整理的Java 的 IO 流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实验一 绘制金刚石图案
- 下一篇: appium连接模拟器时屏幕倒转