JavaOI流02 4.常用的類 4.1文件位元組流輸入流-FileInputStream InputStream抽象類是所有類位元組輸入流的超類 InputStream常用的子類: FileInputStream:文件位元組輸入流 BufferedInputStream:緩衝位元組輸入流 ObjectIn ...
JavaOI流02
4.常用的類
4.1文件位元組流輸入流-FileInputStream
InputStream抽象類是所有類位元組輸入流的超類
InputStream常用的子類:
- FileInputStream:文件位元組輸入流
- BufferedInputStream:緩衝位元組輸入流
- ObjectInputStream:對象位元組輸入流



常用方法:
輸入流的唯一目的是提供通往數據的通道,程式可以通過這個通道讀取文件中的數據。
read方法提供了一個從輸入流讀取數據的基本方法,read方法的格式如下:
返回值 | 方法 | 說明 |
---|---|---|
int | read( ) | 從輸入流中讀取數據的下一個位元組 |
int | read(byte[ ] b) | 從輸入流中讀取一定數量的位元組,並將其存儲在緩衝區數組 b 中。以整數形式返回讀取的位元組數。 |
int | read(byte[ ] b, int off, int len) | 將輸入流中最多 len 個數據位元組讀入 byte 數組。嘗試讀取 len 個位元組,但讀取的位元組也可能小於該值。以整數形式返回實際讀取的位元組數。 |
void | close( ) | 關閉流 |
註:read方法在從輸入流中讀取源中的數據時,如果到達源的末尾,便會返回-1。
FileInputStream流順序地讀取文件,只要不關閉流,每次調用read方法就順序的讀取源中其餘的內容,直至源的末尾或流被關閉。
例子:
package li.io.inputstream_;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
//演示FileInputStream的使用(位元組輸入流 文件-->程式)
public class FileInputStream_ {
public static void main(String[] args) {
}
/**
* 演示讀取文件
* read():單個位元組的讀取,效率較低
*/
@Test
public void readFile01() {
String filePath = "d:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//創建了FileInputStream對象,用於讀取文件
fileInputStream = new FileInputStream(filePath);
//read()方法:從該輸入流讀取一個位元組的數據。 如果沒有輸入可用,此方法將阻止。
//如果返回-1,則表示達到文件的末尾,表示讀取完畢
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char) readData);//轉成char顯示,因此如果文件裡面有中文字元(每個中文字元占三個位元組),顯示的時候就會出現亂碼
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉文件流,釋放資源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用read(byte[] b)讀取文件,提高效率
*/
@Test
public void readFile02() {
String filePath = "d:\\hello.txt";
//位元組數組
byte[] buf = new byte[8];//一次讀取8個位元組
int readLen = 0;
FileInputStream fileInputStream = null;
try {
//創建了FileInputStream對象,用於讀取文件
fileInputStream = new FileInputStream(filePath);
//read(byte[] b)方法:從該輸入流讀取最多b.length位元組的數據到位元組數組。
//如果返回-1,則表示達到文件的末尾,表示讀取完畢
//如果讀取正常,返回實際讀取的位元組數
while ((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));//顯示
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉文件流,釋放資源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.2文件位元組輸出流-FileOutputStream


常用方法:

例子:FileOutputStream應用實例1
要求:請使用FileOutputStream在a.txt文件中寫入“hello,world”。如果文件不存在,就先創建文件。
(註意:前提是目錄已經存在)
package li.io.outputstream_;
import org.junit.jupiter.api.Test;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStream_ {
public static void main(String[] args) {
}
/**
* 演示使用FileOutputStream將數據寫到文件中,如果該文件不存在,則先創建文件
*/
@Test
public void writeFile() {
String filePath = "d:\\a.txt";
//創建FileOutputStream對象
FileOutputStream fileOutputStream = null;
try {
//得到一個FileOutputStream對象
/*
如果是以new FileOutputStream(filePath)的方式創建對象,
則當寫入內容時,會覆蓋原來的內容
如果是以new FileOutputStream(filePath,true)的方式創建對象,
則當寫入內容時,是在舊內容的末尾追加新內容
*/
fileOutputStream = new FileOutputStream(filePath,true);//以追加的形式去添加新內容
//寫入一個位元組
//fileOutputStream.write('H');
//寫入字元串
String str = "Hello,Jack!";
//String的getBytes方法可以將字元串轉為字元數組
// fileOutputStream.write(str.getBytes());
/*
write(byte[] b,int off,int len)
將len長度的位元組從位於偏移量off的指定位元組輸入寫入此文件輸出流
*/
fileOutputStream.write(str.getBytes(), 0, 4);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
追加前:
追加後:
4.2.1FileInputStream&&FileOutputStream
應用實例2:文件拷貝
要求:完成文件拷貝,將d:\Koala.png拷貝到d:\Koala222.png

在完成程式時,為防止讀取的文件過大,應該是每讀取部分數據,就寫入到指定文件,這裡使用迴圈。
package li.io.outputstream_;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) {
//完成文件拷貝,將c:\\Koala.png拷貝到d:\\
/*
思路分析:
1.創建文件的輸入流,將文件讀入到程式
2.創建文件的輸出流,將讀取到的文件數據寫入指定的文件
*/
String srcFilePath = "d:\\Koala.png";
String destFilePath = "d:\\Koala222.png";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath, true);
//定義一個位元組數組,提高效率
byte[] buf = new byte[1024];//1K
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1) {
//讀取到後,就通過 fileOutputStream寫入到文件
//即,是一邊讀一邊寫)
fileOutputStream.write(buf, 0, readLen);//一定要使用這個方法
}
System.out.println("拷貝成功~");
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉輸入流和輸出流,釋放資源
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


4.3文件字元流FileReader&FileWriter


FileReader和FileWriter介紹:
FileReader和FileWriter是字元流,即按照字元來操作io
-
FileReader相關方法:
- new FileReader(String/File)
- read:每次讀取單個字元,返回該字元,如果到文件末尾返回-1
- read(char[]):批量讀取多個字元到數組,返回讀取到的字元數,如果到文件末尾就返回-1
相關API:
- new String(char[]):將char[]轉換成String
- new String(char[],off,len):將char[]的制定部分轉換成String
-
FileWriter常用方法:
- new FileWriter(File/String):覆蓋模式,相當於流的指針在首端
- new FileWriter(File/String,true):追加模式,相當於流的指針在尾端
- writer(int):寫入單個字元
- writer(char[]):寫入指定數組
- writer(char[],off,len):寫入指定數組的指定部分
- writer(String):寫入整個字元串
- writer(String,off,len):寫入指定字元串的指定部分
相關API:String類:toCharArray:將String轉換成char[]
註意:FileWriter使用後,必須要關閉(close)或刷新(flush),否則寫入不到指定的文件!