不用说了,又是一个经常用到的方法,直接上码了。。。敬请各位小主参阅!若有不足,敬请指正,非常感谢!
文件拷贝源码:
/**
* <strong>文件拷贝</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-base cn.ffp.autotest.base.util FileUtils.java copy, 2016-03-19 12:47:38.299 Exp $
*
* @param srcFile 源文件
* @param desFile 目标文件
* @param override 是否覆盖
*
* @return boolean 成功复制返回 true;否则返回 false
*/
public static boolean copy(String srcFile, String desFile, boolean override) {
boolean success = false; InputStream is = null;
OutputStream os = null; if (StringUtils.isBlank(srcFile) || StringUtils.isBlank(desFile)) {
msg = "参数非法:源文件【" + srcFile + "】或目标文件【" + desFile + "】不可为空、null、Tab、回车等,请确认!";
logger.error(msg);
return success;
} try {
File src = new File(srcFile);
File des = new File(desFile); // 判断源文件是否存在且为文件
if (!src.exists() || src.isDirectory()) {
msg = "文件拷贝失败:源文件【" + srcFile + "】不存在或者为目录,请确认!";
logger.error(msg);
return success;
} // 判断目标文件是否存在且为文件
if (des.isDirectory()) {
msg = "文件拷贝失败:目标文件【" + desFile + "】为目录,请确认!";
logger.error(msg);
return success;
} else if (des.exists() && !override) {
msg = "文件拷贝失败:目标文件【" + desFile + "】已存在,且您未允许覆盖目标文件,请知悉!";
logger.warn(msg);
return success;
} des.delete(); // 创建目标文件所在目录
if (!FileUtils.createFolder(des.getParent())) {
msg = "文件拷贝时创建目标文件【" + desFile + "】目录失败...";
logger.error(msg);
return success;
} is = new FileInputStream(src);
os = new FileOutputStream(des);
int readByte = 0; byte[] buffer = new byte[2048]; while ((readByte = is.read(buffer)) != -1 ) {
os.write(buffer, 0, readByte);
} success = true; msg = "成功拷贝文件:【" + srcFile + "】至【" + desFile + "】,请知悉!";
logger.info(msg);
} catch (FileNotFoundException fnfe) {
msg = "文件拷贝失败:" + fnfe.getMessage();
logger.error(msg, fnfe);
return success;
} catch (IOException ioe) {
msg = "文件拷贝失败:" + ioe.getMessage();
logger.error(msg, ioe);
return success;
} finally {
try {
if (os != null) {
os.close();
} if (is != null) {
is.close();
}
} catch (IOException ioe) {
msg = "文件拷贝成功后,释放资源失败:" + ioe.getMessage();
logger.warn(msg, ioe); return success;
}
} return success;
}
文件拷贝单元测试源码:
@Test(description="public static boolean FileUtils.copy(String srcFile, String desFile, boolean override) ---> 破坏性:文件已存在,且允许覆盖\n", dependsOnMethods="test_copy_desFileExists")
public void test_copy_desFileOverride() {
String srcFile = ConstantInfo.PROJECT_RESOURCES_HOME + "js" + ConstantInfo.OS_FILE_SEPARATOR + "color.js";
String desFile = this.baseDir + "color-copy.js"; Assert.assertTrue(FileUtils.copy(srcFile, desFile, true));
} @Test(description="public static boolean FileUtils.copy(String srcFile, String desFile, boolean override) ---> 破坏性:文件已存在,且不允许覆盖\n", dependsOnMethods="test_copy_ok")
public void test_copy_desFileExists() {
String srcFile = ConstantInfo.PROJECT_RESOURCES_HOME + "js" + ConstantInfo.OS_FILE_SEPARATOR + "color.js";
String desFile = this.baseDir + "color-copy.js"; Assert.assertFalse(FileUtils.copy(srcFile, desFile, false));
} @Test(description="public static boolean FileUtils.copy(String srcFile, String desFile, boolean override) ---> 通过性\n")
public void test_copy_ok() {
String srcFile = ConstantInfo.PROJECT_RESOURCES_HOME + "js" + ConstantInfo.OS_FILE_SEPARATOR + "color.js";
String desFile = this.baseDir + "color-copy.js"; Assert.assertTrue(FileUtils.copy(srcFile, desFile, false));
} @Test(description="public static boolean FileUtils.copy(String srcFile, String desFile, boolean override) ---> 破坏性:文件为目录\n")
public void test_copy_srcIsDirectory() {
String srcFile = ConstantInfo.PROJECT_RESOURCES_MAIN + "js" + ConstantInfo.OS_FILE_SEPARATOR;
String desFile = this.baseDir + "color-copy.js"; Assert.assertFalse(FileUtils.copy(srcFile, desFile, false));
} @Test(description="public static boolean FileUtils.copy(String srcFile, String desFile, boolean override) ---> 破坏性:文件不存在\n")
public void test_copy_srcNotExist() {
String srcFile = ConstantInfo.PROJECT_RESOURCES_MAIN + "js" + ConstantInfo.OS_FILE_SEPARATOR + "color.js";
String desFile = this.baseDir + "color-copy.js"; Assert.assertFalse(FileUtils.copy(srcFile, desFile, false));
}
文件拷贝单元测试源码
至此, Java学习-044-文件拷贝 顺利完结,希望此文能够给初学 Java 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^