一定要注意:
傳入的參數,應該是包含文件名的完整路徑名,
不能把一個文件復制到【文件夾】中,因為【文件夾】本身是不能有輸入輸出流的,只能復制到一個【文件】中,否則會報異常。
以字節流讀寫的三種方式
public class Test {
private static final String FILE_PATH = "e:\\";
private static final String FILE_TYPE = ".exe";
private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;
private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;
private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;
private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;
public static void main(String[] args) throws IOException {
copyByBufStream(FILE_FROM, COPY_FILE_1);
copyByBufArray(FILE_FROM, COPY_FILE_2);
copyByByte(FILE_FROM, COPY_FILE_3);
}
/**
* 利用緩沖輸入流讀取到一個緩沖容器后再寫入。建議使用
*/
public static boolean copyByBufStream(String filePathFrom, String filePathTo) {
try {
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(filePathFrom));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(filePathTo));
int ch = 0;
while ((ch = bufis.read()) != -1) {
bufos.write(ch);
}
bufos.close();
bufis.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 每次讀取一個指定長度數組的字節。建議使用
*/
public static boolean copyByBufArray(String filePathFrom, String filePathTo) {
try {
FileInputStream fis = new FileInputStream(filePathFrom);
FileOutputStream fos = new FileOutputStream(filePathTo);
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
fis.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 一次讀取一個字節。千萬不要用,速度超級慢!
*/
public static boolean copyByByte(String filePathFrom, String filePathTo) {
try {
FileInputStream fis = new FileInputStream(filePathFrom);
FileOutputStream fos = new FileOutputStream(filePathTo);
int ch = 0;
while ((ch = fis.read()) != -1) {
fos.write(ch);
}
fos.close();
fis.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
以字符流讀寫的三種方式
public class Test {
private static final String FILE_PATH = "e:\\";
private static final String FILE_TYPE = ".txt";
private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;
private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;
private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;
private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;
public static void main(String[] args) throws IOException {
//注意,只能復制純文本格式的文件,否則就會出現亂碼
copyByBufLine(FILE_FROM, COPY_FILE_1);
copyByBufArray(FILE_FROM, COPY_FILE_2);
copyByChar(FILE_FROM, COPY_FILE_3);
}
/**
* 一次寫入一行字符
*/
public static boolean copyByBufLine(String filePathFrom, String filePathTo) {
try {
BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
String line = null;
//另外開辟一個緩沖區,存儲讀取的一行數據,返回包含該行內容的字符串,不包含換行符,如果已到達流末尾,則返回【 null】
while ((line = bufr.readLine()) != null) {
bufw.write(line);
bufw.newLine();// 寫入一個行分隔符
bufw.flush();
}
bufr.close();
bufw.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 一次寫入指定個數的字符
*/
public static boolean copyByBufArray(String filePathFrom, String filePathTo) {
try {
BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
char[] buf = new char[1024];
int len = 0;
while ((len = bufr.read(buf)) != -1) {
bufw.write(buf, 0, len);
bufw.flush();
len = 0;
}
bufr.close();
bufw.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 一次寫入一個字符
*/
public static boolean copyByChar(String filePathFrom, String filePathTo) {
try {
BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
int ch = 0;
while ((ch = bufr.read()) != -1) {
bufw.write(ch);//寫入單個字符
}
bufr.close();
bufw.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
