public static boolean copyFromSourceToDestFile(File sourceFile, //原路径
File destFile,//目标路径
boolean move )//true 为 移动 false为复制
throws IOException {
createPath(destFile, true);
InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
if (move) {
sourceFile.delete();
}
return true;
}
public static void createPath(File file, boolean asFile) throws IOException {
String path = file.getAbsolutePath();
String dirPath;
if (asFile) {
dirPath = path.substring(0, path.lastIndexOf(File.separator));
} else {
dirPath = path;
}
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
if (asFile) {
file.createNewFile();
}
File destFile,//目标路径
boolean move )//true 为 移动 false为复制
throws IOException {
createPath(destFile, true);
InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
if (move) {
sourceFile.delete();
}
return true;
}
public static void createPath(File file, boolean asFile) throws IOException {
String path = file.getAbsolutePath();
String dirPath;
if (asFile) {
dirPath = path.substring(0, path.lastIndexOf(File.separator));
} else {
dirPath = path;
}
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
if (asFile) {
file.createNewFile();
}
}
我是从ckfinder源码里弄出来的