package com.wdy.charIO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class IOUtils {
// 使用字节流进行复制
public static void copyByFileIOStream(File srcFile,File destFile) throws IOException {
if(srcFile == null || destFile == null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int len = 0;
byte[] byteBuff = new byte[1024];
while((len = fis.read(byteBuff)) != -1) {
fos.write(byteBuff, 0, len);
}
fis.close();
fos.close();
}
// 使用字节缓冲流进行复制
public static void copyByBufferedIOStream(File srcFile,File destFile) throws IOException {
if(srcFile == null || destFile == null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] byteArray = new byte[1024];
int len = 0;
while((len = bis.read(byteArray)) != -1) {
bos.write(byteArray, 0, len);
}
bis.close();
bos.close();
}
// 使用字节转换字符流进行复制
public static void copyByInputStreamRW(File srcFile,File destFile) throws IOException {
if(srcFile == null || destFile ==null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
InputStreamReader isr = new InputStreamReader(new FileInputStream(srcFile));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(destFile));
int len = 0;
char[] cha = new char[1024];
while((len = isr.read(cha)) != -1) {
osw.write(cha, 0, len);
osw.flush();
}
isr.close();
osw.close();
}
// 使用字符流也就是字节转换字符流的子类
// 完全可以使用InputStreamReader 和 OutputStreamWriter
// 或者说InputStreamReader 和 OutputStreamWriter的别名就是FileReader和FileWriter
public static void copyByFileRW(File srcFile, File destFile) throws IOException {
if(srcFile == null || destFile == null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
FileReader fr = new FileReader(srcFile);
FileWriter fw = new FileWriter(destFile);
int len = 0;
char[] chr = new char[1024];
while((len = fr.read(chr)) != -1) {
fw.write(chr, 0, len);
fw.flush();
}
fr.close();
fw.close();
}
// 使用字符缓冲流进行复制
public static void copyByBufferedRW(File srcFile, File destFile) throws IOException {
if(srcFile == null || destFile == null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
BufferedReader br = new BufferedReader(new FileReader(srcFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
int len = 0;
char[] chr = new char[1024];
while((len = br.read(chr)) != -1) {
bw.write(chr,0,len);
bw.flush();
}
br.close();
bw.close();
}
// 使用字符转换流特有的方法进行复制操作
public static void copyByNewBufferedRW(File srcFile, File destFile) throws IOException {
if(srcFile == null || destFile == null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
BufferedReader br = new BufferedReader(new FileReader(srcFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
String str = null;
while((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
建议使用带有BufferedXXX的方法。复制数据时,如果我们用记事本打开并能够读懂,就用字符流,否则用字节流。
复制文件夹以及文件夹中的内容到指定的文件夹,代码如下
package com.wdy.copyFloder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {
public static void copyFolder(File srcFile, File destFile) throws IOException {
if(srcFile == null || destFile == null) {
return;
}
// 如果目的地文件夹不存在就创建
createFolder(destFile);
// 遍历源文件夹并且进行复制
traverseCopy(srcFile,destFile);
}
private static void traverseCopy(File srcFile,File destFile) throws IOException {
if(srcFile.isDirectory()) {
File newFile = new File(destFile,srcFile.getName());
createFolder(newFile);
File[] listFile = srcFile.listFiles();
for(File file : listFile) {
traverseCopy(file,newFile);
}
}else {
File newDestFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newDestFile);
}
}
private static void copyFile(File srcFile, File destFile) throws IOException {
if(srcFile == null || destFile == null || srcFile.isDirectory() || destFile.isDirectory()) {
return;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len = 0;
byte[] byteBuff = new byte[1024];
while((len = bis.read(byteBuff)) != -1) {
bos.write(byteBuff,0,len);
}
bis.close();
bos.close();
}
private static void createFolder(File file) {
if(!file.isFile()) {
file.mkdirs();
}
}
}