- 1.FileReader配合FileWriter进行文本文件的读写。
/**
* 利用FileReader和String srcPath = "src/main/java/health.txt";进行文件copy
* 默认采用utf-8.挨个字符读取,然后抓换成对应的utf-8的编码十进制进行显示。
* 文本后缀文件可以识别出来每一个字符的编码,从而在存储时候,转换成对应的文字,这是
* 操作系统完成的。
*/
@Test
public void testIoCopyFileWriter() {
//String srcPath = "src/main/java/test.pptx";
//String destPath = "src/main/java/copy.pptx";
String srcPath = "src/main/java/health.txt";
String destPath = "src/main/java/new.txt";
try {
fileReader = new FileReader(srcPath);
fileWriter = new FileWriter(destPath, true);
//一个字符一个字符读
char[] chars = new char[1024];
int c;
while ((c = fileReader.read(chars)) != -1) {
System.out.println(c);
fileWriter.write(chars,0,c);
}
fileWriter.flush();
System.out.println("copy successful");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader!=null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileWriter!=null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 2.通过FileInputStream配合FileOutputStream进行字节流的文件copy,是copy文件的核心API
public void testIoCopyFileOutputStream() {
//String srcPath = "src/main/java/test.pptx";
//String destPath = "src/main/java/new/old/haha";
String srcPath = "src/main/java/health.txt";
String destPath = "src/main/java/copy.txt";
try {
File file = new File(srcPath);
if (file.exists()) {
System.out.println("源文件存在,可以复制");
} else {
System.out.println("源文件不存在,开始创建");
boolean newFile = file.createNewFile();
if (newFile) {
System.out.println("源文件创建成功");
}
}
fileInputStream = new FileInputStream(file);
File destFile = new File(destPath);
if (destFile.exists()) {
System.out.println("缓存文件存在,需要删除");
boolean delete = destFile.delete();
if (delete) {
System.out.println("缓存文件删除成功");
}
}else {
System.out.println("没有缓存文件");
if (destFile.getParentFile().mkdirs()){
System.out.println("缓存文件的目录创建成功");
destFile.createNewFile();
}
}
fileOutputStream = new FileOutputStream(destFile);
byte[] readByte = new byte[1024];
int endFlags = 0;
while ((endFlags = fileInputStream.read(readByte)) != -1) {
fileOutputStream.write(readByte, 0, readByte.length);
}
System.out.println("完成了复制");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 3.通过BufferdReader配合BufferedWriter进行文本文件的copy
/**
* 字符缓冲输出流进行copy
* BufferedWriter,或者说Writer类,只适合处理文本文件,也就是.txt文件的copy。
* 永远不要用Writer进行非文本文件的copy
*/
public void testIoCopyBufferedWriter() throws IOException {
// String srcPath = "src/main/java/test.xlsx";
// String destPath = "src/main/java/new/old/haha/test.xlsx";
// String srcPath = "src/main/java/test.jpeg";
// String destPath = "src/main/java/new/old/haha/test.jpeg";
// String srcPath = "src/main/java/test.pptx";
// String destPath = "src/main/java/new/old/haha/test.pptx";
// String srcPath = "src/main/java/health.txt";
// String destPath = "src/main/java/new/old/haha/copy.txt";
String srcPath = "src/main/java/test.txt";
String destPath = "src/main/java/new/old/haha/test.txt";
File file = new File(srcPath);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
File destFile = new File(destPath);
if (destFile.exists()) {
boolean delete = destFile.delete();
if (delete) {
System.out.println("存在脏数据,已删除");
}
}else if (!destFile.getParentFile().exists()) {
boolean mkdirs = destFile.getParentFile().mkdirs();
if (mkdirs) {
System.out.println("创建上级目录成功");
}
}
FileWriter fileWriter = new FileWriter(destFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
char[] chars = new char[1024];
int count;
while ((count = bufferedReader.read(chars))>0) {
bufferedWriter.write(chars,0,count);
}
bufferedWriter.flush();
bufferedWriter.close();
bufferedReader.close();
}
- 4.通过BufferedOutputStream配合BufferedInputStream进行文件的高效copy,非文本文件copy最推荐的API
/**
* 字节缓冲输出流进行copy
*/
@Test
public void testIoCopyFileBufferStream() throws IOException {
// String srcPath = "src/main/java/test.xlsx";
// String destPath = "src/main/java/new/old/haha/test.xlsx";
// String srcPath = "src/main/java/test.jpeg";
// String destPath = "src/main/java/new/old/haha/test.jpeg";
// String srcPath = "src/main/java/test.pptx";
// String destPath = "src/main/java/new/old/haha/test.pptx";
String srcPath = "src/main/java/health.txt";
String destPath = "src/main/java/new/old/haha/copy.txt";
File file = new File(srcPath);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
File destFile = new File(destPath);
if (destFile.exists()) {
System.out.println("脏数据存在,删除文件");
destFile.delete();
} else {
if (destFile.getParentFile().mkdirs()){
System.out.println("成功创建了目的文件路径");
}
}
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] bytes = new byte[18090];
//TODO 这儿需要注意,从流读入的数据需要放在bytes这个缓冲数组中
// while (bufferedInputStream.read(bytes)!=-1) {
// //TODO 使用bytes.length,对于非文本文件是可以进行copy,但对于.txt文件,会乱码
// bufferedOutputStream.write(bytes,0,bytes.length);
// }
int size;
while ((size = bufferedInputStream.read(bytes))!=-1) {
bufferedOutputStream.write(bytes,0,size);
}
//读写数据完成后,flush一下确保数据全部写入文件
System.out.println("copy success");
bufferedOutputStream.flush();
bufferedInputStream.close();
fileInputStream.close();
bufferedOutputStream.close();
fileOutputStream.close();
}
综上,就是文件copy的经典api了。