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();
}
}
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();
}
}
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();
}
}
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();
}
}