文件夹的复制是在文件复制基础上设计的,首先判断是不是文件夹,是就创建新目录,不是就复制文件,有3个文件复制方法,都能复制文件。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class FilesCopy {
/**
* 完成文件拷贝
*
* @param source
* 源文件
* @param targetDir
* 目标目录
*/
public void copys(File source, File targetDir) {
File target = new File(targetDir, source.getName());
if (target.isDirectory()) {
File[] files = source.listFiles();
if (files != null) {
for (File file : files) {
// int spaceCount = file.getAbsolutePath().split("\\\\").length;
// for (int i = 0; i < spaceCount; i++) {
// System.out.print("--");
// }
// System.out.println(file.getName());
if (file.isDirectory()) {
// find(file);
// target = new File(target, file.getName());
if (!new File(target, file.getName()).exists()) {
new File(target, file.getName()).mkdirs();
}
copys(file, target);
} else {
copy(file, target);
}
}
} else {
// target.mkdirs();
}
}else {
copy(source, targetDir);
}
}
public static boolean MyisDirectory(String s) {
if (s.matches(".+\\.[a-z]{3,4}")) {
return false;
} else {
return true;
}
}
public void fileCopy(File source, File targetDir) throws IOException {
File target = new File(targetDir, source.getName());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b, 0, len)) != -1) {
bos.write(b, 0, len);
;
}
bos.close();
bis.close();
}
public void copy2(File source,File targetDir) throws IOException{
File target = new File(targetDir,source.getName());
//获取源文件的随机访问文件流
RandomAccessFile raf_in = new RandomAccessFile(source, "r");
//获取目标文件的随机访问文件流
RandomAccessFile raf_out = new RandomAccessFile(target, "rw");
byte[] b = new byte[1024];
int len = 0;
while((len = raf_in.read(b)) != -1){
raf_out.write(b, 0, len);
}
raf_out.close();
raf_in.close();
}
public void copy(File source, File targetDir) {
// 根据指定的目录以及源文件名称构建新的file对象
if (!targetDir.exists()) {
targetDir.mkdirs();
}
File target = new File(targetDir, source.getName());
InputStream is = null;
OutputStream os = null;
try {
// 创建源文件的输入流
is = new FileInputStream(source);
// 创建目标文件的输出流
os = new FileOutputStream(target);
// 声明字节缓冲区
byte[] b = new byte[1024];
// 声明临时变量存储每次读取的真实长度
int len = 0;
System.out.println("开始拷贝...");
while ((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
System.out.println("拷贝完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File dir1 = new File("C:\\Users\\Administrator\\Desktop\\刘庆.zip");
File dir2 = new File("C:\\Users\\Administrator\\Desktop\\3");
new FilesCopy().copys(dir1, dir2);
}
}