public static void CopyFile(File src, File dest) throws IOException; if (src.isDirectory()) {
throw new IOException("这是一个文件夹");
} InputStream iStream = new FileInputStream(src);
OutputStream oStream = new FileOutputStream(dest); byte[] flush = new byte[1024];
int len = 0; while (-1 != (len = iStream.read(flush))) {
oStream.write(flush, 0, len);
} out.flush(); oStream.close();
iStream.close(); public static void CopyFile(File src, File dest) throws IOException {
if (src.isDirectory()) {
throw new IOException("这是一个文件夹");
}
InputStream iStream = new FileInputStream(src);
OutputStream oStream = new FileOutputStream(dest);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = iStream.read(flush))) {
oStream.write(flush, 0, len);
}
oStream.flush();
oStream.close();
iStream.close();
} public static void CopyFile(String srcPath, String destPath) throws IOException {
CopyFile(new File(srcPath), new File(destPath));
}public static void Copydirs(File src, File dest) throws IOException public static void Copydirs(File src, File dest) throws IOException {
if (src.isDirectory()) {
dest = new File(dest, src.getName());
}
CopyDir(src, dest);
} private static void CopyDir(File src, File dest) throws IOException {
if (src.isFile())
CopyFile(src, dest); // 如果是文件就拷贝
else if (src.isDirectory()) {
dest.mkdirs();
for (File sub : src.listFiles()) {
CopyDir(sub, new File(dest, sub.getName()));
}
}
} public static void Copydirs(String srcPath, String destPath) throws IOException {
Copydirs(new File(srcPath), new File(destPath));
}package cn.hxh.io.stream;
import java.io.*;
public class CopyUtil {
public static void main(String[] args) {
}
public static void CopyFile(String srcPath, String destPath) throws IOException {
CopyFile(new File(srcPath), new File(destPath));
}
public static void CopyFile(File src, File dest) throws IOException {
if (src.isDirectory()) {
throw new IOException("这是一个文件夹");
}
InputStream iStream = new FileInputStream(src);
OutputStream oStream = new FileOutputStream(dest);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = iStream.read(flush))) {
oStream.write(flush, 0, len);
}
oStream.flush();
oStream.close();
iStream.close();
}
public static void Copydirs(String srcPath, String destPath) throws IOException {
Copydirs(new File(srcPath), new File(destPath));
}
public static void Copydirs(File src, File dest) throws IOException {
if (src.isDirectory()) {
dest = new File(dest, src.getName());
}
CopyDir(src, dest);
}
private static void CopyDir(File src, File dest) throws IOException {
if (src.isFile())
CopyFile(src, dest); // 如果是文件就拷贝
else if (src.isDirectory()) {
dest.mkdirs();
for (File sub : src.listFiles()) {
CopyDir(sub, new File(dest, sub.getName()));
}
}
}
}