新手小白學JAVA IO流 File 字節流 字符流

經過學習以上的幾種流,咱們也能夠拓展嘗試作下文件的複製:
建立包: cn.tedu.out
建立類: TestCopyFile.java
package cn.tedu.out;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;
/**html

  • 本類用於測試文件複製
  • 需求: 接收用戶輸入的文件路徑來進行復制,複製到用戶指定的路徑下
  • */

public class TestCopyFile {java

public static void main(String[] args) {
    //1.提示並接收用戶輸入的要複製的源文件路徑--複製啥
    System.out.println("請輸入源文件路徑:");
    //導包的快捷鍵:Ctrl+Shift+O  接收返回值:Alt+Shift+L
    String f = new Scanner(System.in).nextLine();    
    //2.提示並接收用戶輸入的目標文件所在的位置--複製到哪
    System.out.println("請輸入目標文件的路徑:");
    String t = new Scanner(System.in).nextLine();        
    //3.1根據源文件路徑封裝from文件對象
    File from = new File(f);
    //3.2根據目標文件路徑封裝to文件對象
    File to = new File(t);
    //3.3根據用戶提供的路徑完成文件的複製操做
    //自定義複製文件的方法--字符流--只能操做字符相關文件
    //ZFcopy(from,to);
    //自定義複製文件的方法--字節流--操做啥都行
    ZJcopy(from,to);
}
public static void ZJcopy(File from, File to) {
    InputStream in = null;//[PayPal下載](https://www.gendan5.com/wallet/PayPal.html)定義在整個方法中都生效的字節輸入流對象,注意是局部變量,須要初始化,對象的默認值是null
    OutputStream out = null;//定義在整個方法中都生效的字節輸出流對象,注意是局部變量,須要初始化,對象的默認值是null
    try {
        //1.讀取from文件--操做文件的是字節輸入流
        in = new BufferedInputStream(new FileInputStream(from));
        //2.寫出到to文件--操做文件的是字節輸出流
        out = new BufferedOutputStream(new FileOutputStream(to));
        //3.邊讀邊寫
        int b = 0;//定義變量b,記錄讀取到的數據
        while( (b=in.read()) != -1 ) {//只有沒有數據時,才返回-1,跳出循環,讀寫結束
            out.write(b);//將讀到的數據寫出到文件
        }
        System.out.println("恭喜您!文件複製成功!");
    } catch (IOException e) {
        System.out.println("很抱歉!文件複製失敗!");
        e.printStackTrace();//打印錯誤信息
    }finally {
        //釋放資源
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public static void ZFcopy(File from, File to) {//字符流操做
    Reader in = null ;//定義在整個方法中都生效的字符輸入流對象,注意是局部變量,須要初始化,對象的默認值是null
    Writer out = null;//定義在整個方法中都生效的字符輸出流對象,注意是局部變量,須要初始化,對象的默認值是null
    try {
        //1.讀取from文件--獲取字符流輸入對象
        in = new BufferedReader(new FileReader(from));
        //2.寫出到to文件中--獲取字符流輸出對象
        out = new BufferedWriter(new FileWriter(to));
        //3.邊讀邊寫
        int b = 0;//定義變量,保存讀到的數據
        while( (b = in.read()) != -1 ) {//當沒有數據時,返回-1,讀取循環結束
            out.write(b);//將本輪循環讀取到的內容寫出
        }
        System.out.println("恭喜您!文件複製完成!");
    } catch (IOException e) {
        System.out.println("很抱歉!文件複製失敗!");
        e.printStackTrace();//正常打印報錯信息
    }finally {//finally是try-catch結構中必定會執行的部分,也就是若是有必須執行到的代碼,能夠寫在這
        /**
         * 1.流資源必須釋放,釋放的是以前使用過程當中全部流對象
         * 2.關流是有順序的,注意,後出現的流先釋放,爲了避免影響代碼
         * */
        //4.釋放資源
        try {//用來關閉字符輸出流
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {//用來關閉字符輸入流
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

}學習

相關文章
相關標籤/搜索