1. 程式人生 > 其它 >Java基礎-20(02)總結,遞迴,IO流

Java基礎-20(02)總結,遞迴,IO流

(4)FileInputStream讀取資料

A:操作步驟

a:建立位元組輸入流物件

b:呼叫read()方法

c:釋放資源

B:程式碼體現:

 FileInputStream fis = new FileInputStream("fos.txt");
 //方式1
 int by = 0;
 while((by=fis.read())!=-1) {
 System.out.print((char)by);
 }
 //方式2
 byte[] bys = new byte[1024];
 int len = 0;
 while((len=fis.read(bys))!=-1) {
 System.out.print(new String(bys,0,len));
 }
 fis.close();

(5)案例:2種實現

A:int read():一次讀取一個位元組

package cn.itcast_02;
import java.io.FileInputStream;
import java.io.IOException;
/*
 * 位元組輸入流操作步驟:
 * A:建立位元組輸入流物件
 * B:呼叫read()方法讀取資料,並把資料顯示在控制檯
 * C:釋放資源
 * 
 * 讀取資料的方式:
 * A:int read():一次讀取一個位元組
 * B:int read(byte[] b):一次讀取一個位元組陣列
 */
public class FileInputStreamDemo {
 public static void main(String[] args) throws IOException {
 // FileInputStream(String name)
 // FileInputStream fis = new FileInputStream("fis.txt");
 FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
 // // 呼叫read()方法讀取資料,並把資料顯示在控制檯
 // // 第一次讀取
 // int by = fis.read();
 // System.out.println(by);
 // System.out.println((char) by);
 //
 // // 第二次讀取
 // by = fis.read();
 // System.out.println(by);
 // System.out.println((char) by);
 //
 // // 第三次讀取
 // by = fis.read();
 // System.out.println(by);
 // System.out.println((char) by);
 // // 我們發現程式碼的重複度很高,所以我們要用迴圈改進
 // // 而用迴圈,最麻煩的事情是如何控制迴圈判斷條件呢?
 // // 第四次讀取
 // by = fis.read();
 // System.out.println(by);
 // // 第五次讀取
 // by = fis.read();
 // System.out.println(by);
 // //通過測試,我們知道如果你讀取的資料是-1,就說明已經讀取到檔案的末尾了
 // 用迴圈改進
 // int by = fis.read();
 // while (by != -1) {
 // System.out.print((char) by);
 // by = fis.read();
 // }
 // 最終版程式碼
 int by = 0;
 // 讀取,賦值,判斷
 while ((by = fis.read()) != -1) {
 System.out.print((char) by);
 }
 // 釋放資源
 fis.close();
 }
}
 B:int read(byte[] b):一次讀取一個位元組陣列
package cn.itcast_02;
import java.io.FileInputStream;
import java.io.IOException;
/*
 * 一次讀取一個位元組陣列:int read(byte[] b)
 * 返回值其實是實際讀取的位元組個數。
 */
public class FileInputStreamDemo2 {
 public static void main(String[] args) throws IOException {
 // 建立位元組輸入流物件
 // FileInputStream fis = new FileInputStream("fis2.txt");
 FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
 // 讀取資料
 // 定義一個位元組陣列
 // 第一次讀取
 // byte[] bys = new byte[5];
 // int len = fis.read(bys);
 // // System.out.println(len);
 // // System.out.println(new String(bys));
 // // System.out.println(new String(bys, 0, len));
 // System.out.print(new String(bys, 0, len));
 //
 // // 第二次讀取
 // len = fis.read(bys);
 // // System.out.println(len);
 // // System.out.println(new String(bys));
 // // System.out.println(new String(bys, 0, len));
 // System.out.print(new String(bys, 0, len));
 //
 // // 第三次讀取
 // len = fis.read(bys);
 // // System.out.println(len);
 // // System.out.println(new String(bys));
 // // System.out.println(new String(bys, 0, len));
 // System.out.print(new String(bys, 0, len));
 //
 // // 第四次讀取
 // len = fis.read(bys);
 // // System.out.println(len);
 // // System.out.println(new String(bys, 0, len));
 // System.out.print(new String(bys, 0, len));
 // // 程式碼重複了,用迴圈改進
 // // 但是,我不知道結束條件
 // // len = fis.read(bys);
 // // System.out.println(len);
 // // len = fis.read(bys);
 // // System.out.println(len);
 // // 如果讀取到的實際長度是-1,就說明沒有資料了
 // byte[] bys = new byte[115]; // 0
 // int len = 0;
 // while ((len = fis.read(bys)) != -1) {
 // System.out.print(new String(bys, 0, len));
 // // System.out.print(new String(bys)); //千萬要帶上len的使用
 // }
 // 最終版程式碼
 // 陣列的長度一般是1024或者1024的整數倍
 byte[] bys = new byte[1024];
 int len = 0;
 while ((len = fis.read(bys)) != -1) {
 System.out.print(new String(bys, 0, len));
 }
 // 釋放資源
 fis.close();
 }
}

A:複製文字檔案

package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 複製文字檔案。
 * 
 * 資料來源:從哪裡來
 * a.txt -- 讀取資料 -- FileInputStream 
 * 
 * 目的地:到哪裡去
 * b.txt -- 寫資料 -- FileOutputStream
 * 
 * java.io.FileNotFoundException: a.txt (系統找不到指定的檔案。)
 * 
 * 這一次複製中文沒有出現任何問題,為什麼呢?
 * 上一次我們出現問題的原因在於我們每次獲取到一個位元組資料,就把該位元組資料轉換為了字元資料,然後輸出到控制檯。
 * 而這一次呢?確實通過IO流讀取資料,寫到文字檔案,你讀取一個位元組,我就寫入一個位元組,你沒有做任何的轉換。
 * 它會自己做轉換。
 */
public class CopyFileDemo {
 public static void main(String[] args) throws IOException {
 // 封裝資料來源
 FileInputStream fis = new FileInputStream("a.txt");
 // 封裝目的地
 FileOutputStream fos = new FileOutputStream("b.txt");
 int by = 0;
 while ((by = fis.read()) != -1) {
 fos.write(by);
 }
 // 釋放資源(先關誰都行)
 fos.close();
 fis.close();
 }
}

B:把c盤下的a.txt的內容複製到d盤下的b.txt中

package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把c盤下的a.txt的內容複製到d盤下的b.txt中
 * 
 * 資料來源:
 *  c:\a.txt -- 讀取資料-- FileInputStream
 * 目的地:
 *  d:\b.txt -- 寫出資料 -- FileOutputStream
 */
public class CopyFileDemo2 {
 public static void main(String[] args) throws IOException {
 // 封裝資料來源
 FileInputStream fis = new FileInputStream("c:\a.txt");
 // 封裝目的地
 FileOutputStream fos = new FileOutputStream("d:\b.txt");
 // 複製資料
 int by = 0;
 while ((by = fis.read()) != -1) {
 fos.write(by);
 }
 // 釋放資源
 fos.close();
 fis.close();
 }
}

c:複製圖片

package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把e:\林青霞.jpg內容複製到當前專案目錄下的mn.jpg中
 * 
 * 資料來源:
 *  e:\林青霞.jpg --讀取資料--FileInputStream
 * 目的地:
 *  mn.jpg--寫出資料--FileOutputStream
 */
public class CopyImageDemo {
 public static void main(String[] args) throws IOException {
 // 封裝資料來源
 FileInputStream fis = new FileInputStream("e:\林青霞.jpg");
 // 封裝目的地
 FileOutputStream fos = new FileOutputStream("mn.jpg");
 // 複製資料
 int by = 0;
 while ((by = fis.read()) != -1) {
 fos.write(by);
 }
 // 釋放資源
 fos.close();
 fis.close();
 }
}

d:複製視訊

package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把e:\哥有老婆.mp4複製到當前專案目錄下的copy.mp4中
 * 
 * 資料來源:
 *  e:\哥有老婆.mp4--讀取資料--FileInputStream
 * 目的地:
 *  copy.mp4--寫出資料--FileOutputStream
 */
public class CopyMp4Demo {
 public static void main(String[] args) throws IOException {
 // 封裝資料來源
 FileInputStream fis = new FileInputStream("e:\哥有老婆.mp4");
 // 封裝目的地
 FileOutputStream fos = new FileOutputStream("copy.mp4");
 // 複製資料
 int by = 0;
 while ((by = fis.read()) != -1) {
 fos.write(by);
 }
 // 釋放資源
 fos.close();
 fis.close();
 }
}

e:計算機是如何識別什麼時候該把兩個位元組轉換為一箇中文呢?

package cn.itcast_03;
import java.util.Arrays;
/*
 * 計算機是如何識別什麼時候該把兩個位元組轉換為一箇中文呢?
 * 在計算機中中文的儲存分兩個位元組:
 *  第一個位元組肯定是負數。
 *  第二個位元組常見的是負數,可能有正數。但是沒影響。
 */
public class StringDemo {
 public static void main(String[] args) {
 // String s = "abcde";
 // // [97, 98, 99, 100, 101]
 String s = "我愛你中國";
 // [-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]
 byte[] bys = s.getBytes();
 System.out.println(Arrays.toString(bys));
 }
}

(6)位元組緩衝區流

A:BufferedOutputStream

package cn.itcast_05;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 通過定義陣列的方式確實比以前一次讀取一個位元組的方式快很多,所以,看來有一個緩衝區還是非常好的。
 * 既然是這樣的話,那麼,java開始在設計的時候,它也考慮到了這個問題,就專門提供了帶緩衝區的位元組類。
 * 這種類被稱為:緩衝區類(高效類)
 * 寫資料:BufferedOutputStream
 * 讀資料:BufferedInputStream
 * 
 * 構造方法可以指定緩衝區的大小,但是我們一般用不上,因為預設緩衝區大小就足夠了。
 * 
 * 為什麼不傳遞一個具體的檔案或者檔案路徑,而是傳遞一個OutputStream物件呢?
 * 原因很簡單,位元組緩衝區流僅僅提供緩衝區,為高效而設計的。但是呢,真正的讀寫操作還得靠基本的流物件實現。
 */
public class BufferedOutputStreamDemo {
 public static void main(String[] args) throws IOException {
 // BufferedOutputStream(OutputStream out)
 // FileOutputStream fos = new FileOutputStream("bos.txt");
 // BufferedOutputStream bos = new BufferedOutputStream(fos);
 // 簡單寫法
 BufferedOutputStream bos = new BufferedOutputStream(
 new FileOutputStream("bos.txt"));
 // 寫資料
 bos.write("hello".getBytes());
 // 釋放資源
 bos.close();
 }
}

B:BufferedInputStream

package cn.itcast_05;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/*
 * 注意:雖然我們有兩種方式可以讀取,但是,請注意,這兩種方式針對同一個物件在一個程式碼中只能使用一個。
 */
public class BufferedInputStreamDemo {
 public static void main(String[] args) throws IOException {
 // BufferedInputStream(InputStream in)
 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
 "bos.txt"));
 // 讀取資料
 // int by = 0;
 // while ((by = bis.read()) != -1) {
 // System.out.print((char) by);
 // }
 // System.out.println("---------");
 byte[] bys = new byte[1024];
 int len = 0;
 while ((len = bis.read(bys)) != -1) {
 System.out.print(new String(bys, 0, len));
 }
 // 釋放資源
 bis.close();
 }
}

(7)案例:4種實現

基本位元組流一次讀寫一個位元組: 共耗時:117235毫秒

基本位元組流一次讀寫一個位元組陣列: 共耗時:156毫秒

高效位元組流一次讀寫一個位元組: 共耗時:1141毫秒

高效位元組流一次讀寫一個位元組陣列: 共耗時:47毫秒

package cn.itcast_06;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把e:\哥有老婆.mp4複製到當前專案目錄下的copy.mp4中
 * 
 * 位元組流四種方式複製檔案:
 * 基本位元組流一次讀寫一個位元組: 共耗時:117235毫秒
 * 基本位元組流一次讀寫一個位元組陣列: 共耗時:156毫秒
 * 高效位元組流一次讀寫一個位元組: 共耗時:1141毫秒
 * 高效位元組流一次讀寫一個位元組陣列: 共耗時:47毫秒 ***必須掌握****
 */
public class CopyMp4Demo {
 public static void main(String[] args) throws IOException {
 //測試時間
 long start = System.currentTimeMillis();
 // method1("e:\哥有老婆.mp4", "copy1.mp4");
 // method2("e:\哥有老婆.mp4", "copy2.mp4");
 // method3("e:\哥有老婆.mp4", "copy3.mp4");
 method4("e:\哥有老婆.mp4", "copy4.mp4");
 long end = System.currentTimeMillis();
 System.out.println("共耗時:" + (end - start) + "毫秒");
 }
 // 高效位元組流一次讀寫一個位元組陣列:
 public static void method4(String srcString, String destString)
 throws IOException {
 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
 srcString));
 BufferedOutputStream bos = new BufferedOutputStream(
 new FileOutputStream(destString));
 byte[] bys = new byte[1024];
 int len = 0;
 while ((len = bis.read(bys)) != -1) {
 bos.write(bys, 0, len);
 }
 bos.close();
 bis.close();
 }
 // 高效位元組流一次讀寫一個位元組:
 public static void method3(String srcString, String destString)
 throws IOException {
 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
 srcString));
 BufferedOutputStream bos = new BufferedOutputStream(
 new FileOutputStream(destString));
 int by = 0;
 while ((by = bis.read()) != -1) {
 bos.write(by);
 }
 bos.close();
 bis.close();
 }
 // 基本位元組流一次讀寫一個位元組陣列
 public static void method2(String srcString, String destString)
 throws IOException {
 FileInputStream fis = new FileInputStream(srcString);
 FileOutputStream fos = new FileOutputStream(destString);
 byte[] bys = new byte[1024];
 int len = 0;
 while ((len = fis.read(bys)) != -1) {
 fos.write(bys, 0, len);
 }
 fos.close();
 fis.close();
 }
 // 基本位元組流一次讀寫一個位元組
 public static void method1(String srcString, String destString)
 throws IOException {
 FileInputStream fis = new FileInputStream(srcString);
 FileOutputStream fos = new FileOutputStream(destString);
 int by = 0;
 while ((by = fis.read()) != -1) {
 fos.write(by);
 }
 fos.close();
 fis.close();
 }
}

A:複製文字檔案

package cn.itcast_04;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把c:\a.txt內容複製到d:\b.txt中
 * 
 * 資料來源:
 *  c:\a.txt -- 讀取資料 -- FileInputStream
 * 目的地:
 *  d:\b.txt -- 寫出資料 -- FileOutputStream
 */
public class CopyFileDemo {
 public static void main(String[] args) throws IOException {
 // 封裝資料來源
 FileInputStream fis = new FileInputStream("c:\a.txt");
 FileOutputStream fos = new FileOutputStream("d:\b.txt");
 // 複製資料
 byte[] bys = new byte[1024];
 int len = 0;
 while ((len = fis.read(bys)) != -1) {
 fos.write(bys, 0, len);
 }
 // 釋放資源
 fos.close();
 fis.close();
 }
}

B:複製圖片

C:複製視訊

package cn.itcast_04;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:把e:\哥有老婆.mp4複製到當前專案目錄下的copy.mp4中
 * 
 * 資料來源:
 *  e:\哥有老婆.mp4--讀取資料--FileInputStream
 * 目的地:
 *  copy.mp4--寫出資料--FileOutputStream
 */
public class CopyMp4Demo {
 public static void main(String[] args) throws IOException {
 // 封裝資料來源
 FileInputStream fis = new FileInputStream("e:\哥有老婆.mp4");
 // 封裝目的地
 FileOutputStream fos = new FileOutputStream("copy.mp4");
 // 複製資料
 byte[] bys = new byte[1024];
 int len = 0;
 while ((len = fis.read(bys)) != -1) {
 fos.write(bys, 0, len);
 }
 // 釋放資源
 fos.close();
 fis.close();
 }
}

3:自學字元流

IO流分類

位元組流:

InputStream

FileInputStream

BufferedInputStream

OutputStream

FileOutputStream

BufferedOutputStream

字元流:

Reader

FileReader

BufferedReader

Writer

FileWriter

BufferedWriter