1.1 IO流的概述和分類
1.1.1 IO流
??IO可以拆開來理解
- I ---- Input -> 輸入 -> 讀取
- O ---- Output -> 輸出 -> 寫出
??讀取和寫出都是針對數(shù)據(jù)而言的, 所以, IO流就是用來處理設(shè)備之間的數(shù)據(jù)傳輸。
常見應(yīng)用
- 文件復(fù)制
- 文件上傳
- 文件下載
1.1.2 IO流的分類
- 按照類型分:
- 字節(jié)流
- 字符流(字符流數(shù)據(jù)通過Windows自帶的記事本軟件打開是可以讀懂里面內(nèi)容的)
- 按照流向分:
- 輸入流: 用來讀取數(shù)據(jù)的:
- 輸出流 : 用來寫出數(shù)據(jù)的
1.2 FileOutputStream寫數(shù)據(jù)
1.2.1 字節(jié)流&字符流的抽象父類
| 字節(jié)流: | |
|---|---|
| InputStream | 字節(jié)輸入流 |
| OutputStream | 字節(jié)輸出流 |
| 字符流: | |
|---|---|
| Reader | 字符輸入流 |
| Writer | 字符輸出流 |
1.2.2 字節(jié)流寫出數(shù)據(jù)
-
字節(jié)流寫數(shù)據(jù)
- OutputStream:此抽象類是表示輸出字節(jié)流的所有類的超類
- FileOutputStream:文件輸出流是用于將數(shù)據(jù)寫入 File
-
構(gòu)造方法:
- FileOutputStream(String name):
- 創(chuàng)建一個向具有指定名稱的文件中寫入數(shù)據(jù)的輸出文件流。
- FileOutputStream(String name):
-
字節(jié)流寫數(shù)據(jù)的步驟:
- 創(chuàng)建字節(jié)輸出流對象
- 調(diào)用寫數(shù)據(jù)的方法
- 釋放資源
1.2.3 案例代碼
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//創(chuàng)建字節(jié)輸出流對象
FileOutputStream fos = new FileOutputStream("a.txt");
/*
* 創(chuàng)建字節(jié)輸出流對象做了這樣的三件事情:
* A:調(diào)用系統(tǒng)功能創(chuàng)建了文件
* B:創(chuàng)建字節(jié)輸出流對象
* C:讓fos這個對象指向a.txt這個文件
*/
//write(int b)
fos.write(65);
fos.write(66);
//最后我們還要做一個事情
//close() 關(guān)閉此文件輸出流并釋放與此流有關(guān)的所有系統(tǒng)資源。
fos.close();
}
}
1.3 FileOutputStream寫數(shù)據(jù)的三種方式.
1.3.1 方法摘要
寫出數(shù)據(jù)的三個方法:
* public void write(int b):一次寫一個字節(jié)
* public void write(byte[] b):一次寫一個字節(jié)數(shù)組
* public void write(byte[] b,int off,int len):一次寫一個字節(jié)數(shù)組的一部分
String類中的方法
* byte[] getBytes() 將字符串轉(zhuǎn)換為字節(jié)數(shù)組
1.3.2 案例代碼
public class FileOutputStreamDemo2 {
public static void main(String[] args) throws IOException {
//創(chuàng)建字節(jié)輸出流對象
//FileOutputStream(String name)
FileOutputStream fos = new FileOutputStream("b.txt");
//new File(name)
// FileOutputStream fos = new FileOutputStream(new File("b.txt"));
//FileOutputStream(File file)
// File file = new File("b.txt");
// FileOutputStream fos = new FileOutputStream(file);
// FileOutputStream fos = new FileOutputStream(new File("b.txt"));
//public void write(int b):一次寫一個字節(jié)
// fos.write(65);
//public void write(byte[] b):一次寫一個字節(jié)數(shù)組
// byte[] bys = {65,66,67,68,69};
// fos.write(bys);
//需求:我如果是一個字符串的數(shù)據(jù),能寫嗎?
//String -- byte[]
//String類中有一個方法:public byte[] getBytes()
// byte[] bys = "ABCDE".getBytes();
// fos.write(bys);
// fos.write("ABCDE".getBytes());
//public void write(byte[] b,int off,int len):一次寫一個字節(jié)數(shù)組的一部分
fos.write("ABCDE".getBytes(),0,3);
//釋放資源
fos.close();
}
}
1.4 FileOutputStream如何實現(xiàn)換行和追加寫數(shù)據(jù)
1.4.1 如何實現(xiàn)數(shù)據(jù)的換行
??不同的操作系統(tǒng),針對換行的符號識別是不一樣的。
- windows:\r\n
- linux:\n
- mac:\r
1.4.2 如何實現(xiàn)數(shù)據(jù)的追加寫入?
用構(gòu)造方法帶第二個參數(shù)是true的情況即可
1.4.3 案例代碼
public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
//創(chuàng)建字節(jié)輸出流對象
//FileOutputStream fos = new FileOutputStream("c.txt");
//FileOutputStream(String name, boolean append)
//如果第二個參數(shù)為 true,則將字節(jié)寫入文件末尾處,而不是寫入文件開始處
FileOutputStream fos = new FileOutputStream("c.txt",true);
//調(diào)用寫數(shù)據(jù)的方法
for(int x=0; x<10; x++) {
fos.write("hello".getBytes());
//加入換行符號
fos.write("\r\n".getBytes());
}
//釋放資源
fos.close();
}
}
1.5 FileOutputStream寫數(shù)據(jù)加入異常處理
1.5.1 try..catch.finally
格式:
try{
可能發(fā)生問題的代碼
}catch(){
處理異常代碼
}finally{
一定會被執(zhí)行的代碼. // 通常用于釋放資源, 做善后的動作
}
1.5.2 案例代碼
public class FileOutputStreamDemo4 {
public static void main(String[] args) {
FileOutputStream fos = null;
try{
//FileOutputStream fos = new FileOutputStream("d.txt");
// fos = new FileOutputStream("z:\\d.txt");
fos = new FileOutputStream("d.txt");
fos.write("hello".getBytes());
}catch(IOException e) {
e.printStackTrace();
}finally {
if(fos!=null) {
//釋放資源
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.6 FileInputStream讀數(shù)據(jù)方式1一次讀取一個字節(jié)
1.6.1 字節(jié)流讀取數(shù)據(jù)的三個步驟
- 字節(jié)流讀數(shù)據(jù)的步驟:
- 創(chuàng)建字節(jié)輸入流對象
- 調(diào)用讀數(shù)據(jù)的方法
- 釋放資源
1.6.2 案例代碼
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
//創(chuàng)建字節(jié)輸入流對象
FileInputStream fis = new FileInputStream("a.txt");
int by;
// 用by不斷的記錄讀取到的每一個數(shù)據(jù)
while((by=fis.read())!=-1) {
System.out.print((char)by);
}
//釋放資源
fis.close();
}
}
1.7 FileInputStream讀數(shù)據(jù)方式2一次讀取一個字節(jié)數(shù)組
1.7.1 方法摘要
public int read(byte[] b):
- 從此輸入流中將最多 b.length 個字節(jié)的數(shù)據(jù)讀入一個 byte 數(shù)組中
- 返回值是讀入緩沖區(qū)的字節(jié)總數(shù),也就是實際的讀取個數(shù)
- 如果因為已經(jīng)到達文件末尾而沒有更多的數(shù)據(jù),則返回 -1。
1.7.2 案例代碼
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
//創(chuàng)建字節(jié)輸入流對象
FileInputStream fis = new FileInputStream("b.txt");
byte[] bys = new byte[1024]; //1024或者1024的整數(shù)倍
int len;
//將數(shù)據(jù)讀取到數(shù)組中, 并用len記錄讀取到的有效字節(jié)個數(shù)
while((len=fis.read(bys))!=-1) {
System.out.print(new String(bys,0,len));
}
//釋放資源
fis.close();
}
}
1.8 字節(jié)流練習(xí)之復(fù)制文本文件
- 需求: 拷貝文本文件
- 分析:
- 創(chuàng)建輸入輸出流對象關(guān)聯(lián)數(shù)據(jù)源和數(shù)據(jù)目的
- 定義字節(jié)數(shù)組,為了提高效率
- 將數(shù)據(jù)通過while循環(huán)不斷讀取到字節(jié)數(shù)組中
- 將數(shù)據(jù)從字節(jié)數(shù)組中取出并寫出
- 釋放資源
1.8.1 案例代碼
public class CopyTxtTest {
public static void main(String[] args) throws IOException {
//封裝數(shù)據(jù)源
FileInputStream fis = new FileInputStream("d:\\窗里窗外.txt");
//封裝目的地
FileOutputStream fos = new FileOutputStream("林青霞.txt");
//讀寫數(shù)據(jù)
//方式1:一次讀取一個字節(jié)
// int by;
// while((by=fis.read())!=-1) {
// fos.write(by);
// }
//方式2:一次讀取一個字節(jié)數(shù)組
byte[] bys = new byte[1024];
int len;
while((len=fis.read(bys))!=-1) {
fos.write(bys, 0, len);
}
//釋放資源
fos.close();
fis.close();
}
}
1.9 字節(jié)流練習(xí)之復(fù)制圖片
public class CopyJpgTest {
public static void main(String[] args) throws IOException {
// 封裝數(shù)據(jù)源
FileInputStream fis = new FileInputStream("d:\\mn.jpg");
// 封裝目的地
FileOutputStream fos = new FileOutputStream("mn.jpg");
// 讀寫數(shù)據(jù)
// 方式1:一次讀取一個字節(jié),一次寫一個字節(jié)(自己練習(xí))
// 方式2:一次讀取一個字節(jié)數(shù)組,一次寫一個字節(jié)數(shù)組的一部分
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 釋放資源
fos.close();
fis.close();
}
}