好久不見的IO流
對IO流的學習,我記得還是初學Java基礎的時候,后來找作業程序中經常看到有些招聘資訊中寫到熟悉IO流,現在想想IO流,真的是一臉懵逼,不說這么多廢話了,IO流這次好好整理一下,
說說IO流的類別
在說流的類別之前,先說說什么是流,流其實就是對輸入輸出設備的抽象,可以把輸入輸出流理解為是一個通道,輸入輸出是相對程式而言的,如果是輸出流,也就是往檔案中寫檔案,而輸入流,則是從檔案中讀取檔案,從三個方面對IO流進行總結,一、位元組流(一般都是xxxStream),二、字符流(xxxRead、xxxWrite),三、緩沖流,其實也可以簡單的分為兩類,分別是輸入流和輸出流,
聊聊檔案
在講解IO流之前,有必要說說檔案的操作,畢竟IO操作大部分也就是檔案嘛,好了先來看看JDK-API檔案吧,首先看看構造方法,

有了構造方法,我們可以通過構造方法創建物件,然后操作一波檔案,創建物件之后,點一下,好家伙,很多可用方法,其實常用的不多,上號,開!

先來看看創建檔案的方法吧
File file = new File("a.txt");
boolean newFile = file.createNewFile();
還能創建目錄呢,不服來戰
File file1 = new File("test");
file1.mkdir();
還有判斷檔案是否存在的方法也很常用
boolean exists = file.exists();
還有好多操作,自己可以試試,有了目錄檔案了,是不是該往里面寫點東西了,來吧~
位元組輸入流
輸入流,是相對于程式而言的,也就是從檔案中讀取檔案,先看構造方法,

// 創建位元組輸入流物件
FileInputStream fis1 = new FileInputStream("a.txt");
// 用單位元組進行讀取
int x = 0;
while ((x = fis1.read()) != -1) {
System.out.println((char) x);
}
這樣一波操作之后,他會把a.txt檔案里的內容讀取出來,但是是單位元組的讀的,單位元組的效率還是比較低的,一般根據實際情況來進行自定義位元組數讀取,下面通過自定義位元組搞一波,
// 創建位元組輸入流物件
FileInputStream fis2 = new FileInputStream("a.txt");
// 用位元組陣列進行讀取
byte[] b = new byte[1024];
int len = 0;
while ((len = fis2.read(b)) != -1) {
System.out.print(new String(b, 0, len));
}
位元組輸出流
輸出流,可以將檔案寫入到檔案中,一般日志檔案寫的比較多,

//創建位元組輸出流物件
FileOutputStream fos = new FileOutputStream("a.txt");
//呼叫write()方法
fos.write("hello".getBytes());
這樣一波操作之后,就可以把“hello”字串轉化為位元組,然后寫入到檔案中,也可以讀取a.txt檔案中的內容,寫入到b.txt檔案中
InputStream in = new FileInputStream("a.txt");
OutputStream os = new FileOutputStream("b.txt");
byte[] bytes = new byte[2];
int n;
while ((n = in.read(bytes)) != -1) {
os.write(bytes, 0, n);
}
字符輸入流
一個漢字大約占兩個位元組,而當用位元組流處理的時候,可能會出現亂碼的情況,字符輸入流FileRead,先來體驗一下,老規矩,先來構造方法,

Reader r = new FileReader("a.txt");
int n;
char[] chars = new char[2];
while ((n = r.read(chars)) != -1) {
String s = new String(chars,0,n);
}
其實跟位元組流差不多,只是這里用char[]字符陣列來進行操作了,
字符輸出流
直接上構造方法

字符寫入的操作,還以讀取a.txt檔案中的內容到b.txt檔案中
Reader r = new FileReader("a.txt");
Writer w = new FileWriter("b.txt");
int n;
char[] chars = new char[3];
while ((n = r.read(chars)) != -1) {
w.write(chars,0,n);
}
位元組緩沖輸入流
老規矩,先看構造方法

可以看出,要傳入一個流的引數,
BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("a.txt"));
// 用位元組陣列進行讀取
byte[] b = new byte[1024];
int len = 0;
while ((len = bis2.read(b)) != -1) {
System.out.print(new String(b, 0, len));
}
位元組緩沖輸出流
位元組緩沖輸出流跟輸入流差不多,可以類比著看,

同樣的以讀取a.txt的檔案到b.txt為例
InputStream inputStream = new FileInputStream("a.txt");
BufferedInputStream bis = new BufferedInputStream(inputStream);
OutputStream outputStream = new FileOutputStream("b.txt");
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] b = new byte[1024];
int n = 0;
while (bis.read(b) != -1) {
bos.write(b);
}
字符緩沖輸入流
字符緩沖輸入流的引數是字符流

Reader in = new FileReader("a.txt");
BufferedReader bufferedReader = new BufferedReader(in);
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
字符緩沖輸出流

同樣的,以讀取a.txt檔案的內容到b.txt為例
Reader in = new FileReader("a.txt");
Writer out = new FileWriter("b.txt");
BufferedReader bufferedReader = new BufferedReader(in);
BufferedWriter bufferedWriter = new BufferedWriter(out);
String str;
while ((str = bufferedReader.readLine()) != null) {
bufferedWriter.write(str);
bufferedWriter.newLine();
}
流的關閉
上面的demo中,為了讓代碼簡介減少重復,就沒有對流進行關閉操作,這里統一說明一下,流在使用后,要進行close()關閉,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/255894.html
標籤:java
