天天看點

java IO流(二)檔案的複制

在上篇我們提到了文本檔案的讀寫,既然能夠讀寫。那麼我們這篇說下檔案的複制,有以下方法:
           

利用FileWriter和FileReader複制文本檔案:

  • 方式一
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class WriterAndReaderTest {
    public static void main(String[] args) throws IOException {
        //被複制的檔案路徑
        String txt = "G:" + File.separator + "Test.txt";
        //複制檔案的路徑
        String copy = "G:" +File.separator + "Test1.txt";
        //讀取
        Reader reader = new FileReader(txt);
        //寫入
        Writer writer = new FileWriter(copy);
        //若不用外部變量接收會丢失資料
        int temp = ;
        while((temp = reader.read()) != -){
            writer.write(temp);
        }
        //關閉流
        reader.close();
        writer.close();
    }
}
           
  • 方式二
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class WriterAndReaderTest1 {
    public static void main(String[] args) throws IOException {
        // 被複制的檔案路徑
        String txt = "G:" + File.separator + "Test.txt";
        // 複制檔案的路徑
        String copy = "G:" + File.separator + "Test1.txt";
        //讀取
        Reader reader = new FileReader(txt);
        //寫入
        Writer writer = new FileWriter(copy);
        //使用字元數組接收
        char[] c = new char[];

        while( reader.read(c) != -){
            writer.write(c);
        }
        //關閉流
        reader.close();
        writer.close();
    }
}
           
  • 方式三(利用字元流緩沖區複制文本檔案<提高效率>)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class WriterAndReaderTest2 {
    public static void main(String[] args) throws IOException {
        // 被複制的檔案路徑
        String txt = "G:" + File.separator + "Demo.txt";
        // 複制檔案的路徑
        String copy = "G:" + File.separator + "Demo1.txt";
        // 讀取
        Reader reader = new FileReader(txt);
        // 寫入
        Writer writer = new FileWriter(copy);
        //建立緩沖區對象
        BufferedReader br = new BufferedReader(reader);
        BufferedWriter bw = new BufferedWriter(writer);
        //讀取行,直到傳回null
        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
        }
        關閉緩沖區就是關閉緩沖區中的流對象
        bw.close();
        br.close();

    }
}
           
雖然字元流可以複制文本檔案,但比如圖檔之類的複制就會導緻檔案無法打開或者損壞,那我們就需要用到位元組流來讀寫。可以使用以下方法:
           

利用FileInputStream和FileOutputStream複制圖檔:

  • 方式一
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputOutputStreamTest {
    public static void main(String[] args) throws IOException {
        //被複制圖檔的路徑
        String png = "G:" + File.separator + "01.png";
        //複制圖檔的路徑
        String copy = "G:" + File.separator + "02.png";
        //讀取
        InputStream in = new FileInputStream(png);
        //寫入
        OutputStream os = new FileOutputStream(copy);

        int temp = ; 
        while((temp = in.read()) != -){
            os.write(temp);
        }
        //關閉流
        os.close();
        in.close();
    }
}
           
  • 方式二(利用位元組流緩沖區複制圖檔<提高效率>)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InputOutputStreamTest1 {
    public static void main(String[] args) throws IOException {
        //被複制圖檔的路徑
        String png = "G:" + File.separator + "01.png";
        //複制圖檔的路徑
        String copy = "G:" + File.separator + "02.png";
        //讀取
        InputStream in = new FileInputStream(png);
        //寫入
        OutputStream os = new FileOutputStream(copy);
        //建立緩沖區
        BufferedInputStream bis = new BufferedInputStream(in);
        BufferedOutputStream bos = new BufferedOutputStream(os);

        byte[] buf = new byte[];
        int temp = ;
        while((temp = bis.read(buf)) != -){
            bos.write(buf,  ,temp);
        }

        //關閉緩沖區
        bos.close();
        bis.close();
    }
}
           

總結

看了上面的執行個體相信大家應該明白了,到底該什麼時候用字元流和位元組流了
    1:位元組流讀取二進制檔案(如圖檔、mp3)
    2:字元流用來操作字元檔案較多的文本
用緩沖區能夠在讀寫大檔案的時候有效提高效率