Skip to content

Instantly share code, notes, and snippets.

@binjoo
Last active December 12, 2018 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save binjoo/5458076 to your computer and use it in GitHub Desktop.
Save binjoo/5458076 to your computer and use it in GitHub Desktop.
JAVA:复制文件和文件夹
package com.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/***
*
* @ClassName CopyUtils
* @Description TODO
* @author liangjian
* @date Apr 25, 2013 2:55:40 PM
*
*/
public class CopyUtils {
/**
* 复制单个文件
*
* @param oldPath
* 原文件路径
* @param newPath
* 复制后路径
*/
public static boolean copyFile(String oldPath, String newPath) {
boolean result = false;
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
String _newPath = newPath.substring(0, newPath.lastIndexOf("\\"));
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
File newFile = new File(_newPath);
if(!newFile.exists()){
newFile.mkdirs();
System.out.println("create");
}
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
if(new File(newPath).exists()){
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 复制文件夹
*
* @param oldPath
* 原文件路径
* @param newPath
* 复制后路径
*/
public static boolean copyFolder(String oldPath, String newPath) {
boolean result = false;
try {
(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
if(new File(newPath).exists()){
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment