轉載請註明出處:blog.csdn.net/linglongxin… 【DylanAndroid的csdn部落格】
#方式一(字元流讀寫複製檔案,僅限文字檔案)
/**
* 字元流讀寫複製檔案
*
* @param src 原始檔
* @param out 目標檔案
*/
public static void FileReaderFileWriter(String src, String out) {
FileWriter fileWriter = null;
FileReader fileReader = null;
try {
//建立一個可以往檔案中寫入字元資料的字元輸出流物件。
/*
* 既然是往一個檔案中寫入文字資料,那麼在建立物件時,就必須明確該檔案(用於儲存資料的目的
* 地)。
*
* 如果檔案不存在,則會自動建立。
* 如果檔案存在,則會被覆蓋。
*
* 如果建構函式中加入true,可以實現對檔案進行續寫!
*/
fileWriter = new FileWriter(out);
//1,建立讀取字元資料的流物件。
/*
* 在建立讀取流物件時,必須要明確被讀取的檔案。一定要確定該檔案是存在的。
*
* 用一個讀取流關聯一個已存在檔案。
*/
fileReader = new FileReader(src);
//建立一個臨時容器,用於快取讀取到的字元。
char[] chars = new char[1024];
//定義一個變數記錄讀取到的字元數,(其實就是往陣列裡裝的字元個數 )
int num = 0;
while ((num = fileReader.read(chars)) != -1) {
fileWriter.write(chars, 0, num);
fileWriter.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}複製程式碼
#方式二(字元流緩衝區讀寫檔案-高效,僅限文字檔案)
/**
* 字元流緩衝區讀寫檔案
* 效率比較高
* @param src 原始檔
* @param out 目標檔案
*/
public static void BufferReaderBufferWriter(String src, String out) {
BufferedWriter bufferedWriter = null;
BufferedReader bufferedReader = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(out));
bufferedReader = new BufferedReader(new FileReader(src));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 位元組流讀寫複製檔案
* @param src 原始檔
* @param out 目標檔案
*/
public static void InputStreamOutputStream(String src, String out) {
FileOutputStream outputStream = null;
FileInputStream inputStream = null;
try {
outputStream = new FileOutputStream(out);
inputStream = new FileInputStream(src);
byte[] bytes = new byte[1024];
int num = 0;
while ((num = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, num);
outputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}複製程式碼
#方式三(位元組流讀寫複製檔案)
/**
* 位元組流讀寫複製檔案
* @param src 原始檔
* @param out 目標檔案
*/
public static void InputStreamOutputStream(String src, String out) {
FileOutputStream outputStream = null;
FileInputStream inputStream = null;
try {
outputStream = new FileOutputStream(out);
inputStream = new FileInputStream(src);
byte[] bytes = new byte[1024];
int num = 0;
while ((num = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, num);
outputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}複製程式碼
#方式四(位元組緩衝流讀寫複製檔案)
/**
* 位元組緩衝流讀寫複製檔案
* @param src 原始檔
* @param out 目標檔案
*/
public static void BufferInputStreamBufferOutputStream(String src, String out) {
BufferedOutputStream bufferedOutputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(out));
bufferedInputStream = new BufferedInputStream(new FileInputStream(src));
byte[] bytes = new byte[1024];
int num = 0;
while ((num = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, num);
bufferedOutputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedOutputStream.close();
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}複製程式碼
#執行程式碼
public static void main(String args[]) {
String src = "E:\kejiang\IdeaProjects\JavaProjectTest\src\io\CopyTextFileTest.java";
String out = "E:\kejiang\IdeaProjects\JavaProjectTest\src\io\CopyTextFileTest_Copy.txt";
// FileReaderFileWriter(src, out);
// BufferReaderBufferWriter(src, out);
// InputStreamOutputStream(src, out);
BufferInputStreamBufferOutputStream(src, out);
}複製程式碼
#執行效果