修改后的CopyFile类
package com.test; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* 通过一定的逻辑判断具体调用哪个方法
* @author Dawn
*
*/
public class CopyFile {
/**
* 在同目录下拷贝文件,加上文件名的命名逻辑
* @param srcFilePath 源文件路径
* @param destFilePath 目标文件路径
*/
public static void copyFileSamePath(String srcFilePath,String destFilePath){
File srcFile=new File(srcFilePath);
File destFile=new File(destFilePath);
if(!srcFile.exists()){
System.out.println("源文件不存在");
return;
}
if(!(srcFile.getPath().equals(destFile.getPath()))){
System.out.println("方法调用错误:只复制同一个目录下的文件");
return;
}
if(!srcFile.isFile()){
System.out.println("方法调用错误:源文件不是单个的文件");
return;
}
File newFile=naming(destFile);
try{
newFile.createNewFile();
}catch(IOException e){
e.printStackTrace();
}
copyData(srcFile,newFile);
}
/**
* 用来给同目录下的新建的文件和文件夹命名
* @param destFile 目标File类的引用
* @return 返回更名后的File类的引用
*/
private static File naming(File destFile){
File newFile=null;
int increasing=2;
String folder=destFile.getParent();
String fileName="复件"+destFile.getName();
String newPath=folder+File.separator+fileName;
newFile=new File(newPath);
while(newFile.exists()){
fileName="复件"+increasing++ +" "+destFile.getName();
newPath=folder+File.separator+fileName;
newFile=new File(newPath);
}
return newFile;
}
/**
* 用来传输数据
* @param srcFile 源文件的File类引用
* @param destFile 目标文件的File类引用
*/
private static void copyData(File srcFile,File destFile){
try{
FileInputStream reader=new FileInputStream(srcFile);
FileOutputStream writer=new FileOutputStream(destFile);
int length=0;
byte[] dataBytes=new byte[4096];
while((length=reader.read(dataBytes))!=-1){
writer.write(dataBytes,0,length);
}
reader.close();
writer.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
/**
* 在不同目录下拷贝文件
* @param srcFilePath 源文件路径
* @param destFilePath 目标文件路径
* @param overlay 是否覆盖
*/
public static void copyFileDifferentPath(String srcFilePath,String destFilePath,boolean overlay){
File srcFile=new File(srcFilePath);
File destFile=new File(destFilePath);
if(!srcFile.exists()){
System.out.println("源文件不存在");
return;
}
if(srcFile.getPath().equals(destFile.getPath())){
System.out.println("方法调用错误:只能复制到不同的目录下");
return;
}
if(!srcFile.isFile()){
System.out.println("方法调用错误:只能复制文件");
return;
}
if(!(srcFile.getName().equals(destFile.getName()))){
System.out.println("输入错误:文件名不一样");
return;
}
if(destFile.exists()){
if(overlay){
destFile.delete();
}else{
System.out.println("文件重名");
return;
}
}
try{
destFile.createNewFile();
}catch(IOException e){
e.printStackTrace();
return;
}
copyData(srcFile,destFile);
}
/**
* 在不同的目录下拷贝文件夹
* @param srcFolderPath 源文件夹路径
* @param destFolderPath 目标文件夹路径
* @param overlay 是否覆盖
*/
public static void copyFolderDifferentPath(String srcFolderPath,String destFolderPath,boolean overlay){
File srcFolder=new File(srcFolderPath);
File destFolder=new File(destFolderPath);
if(!srcFolder.exists()){
System.out.println("源文件不存在");
return;
}
if(srcFolder.getPath().equals(destFolder.getPath())){
System.out.println("方法调用错误:只能复制到不同的目录下");
return;
}
if(!srcFolder.isDirectory()){
System.out.println("方法调用错误:只能复制文件夹");
return;
}
if(!(srcFolder.getName().equals(destFolder.getName()))){
System.out.println("输入错误:文件名不一样");
return;
}
if(destFolder.exists()){
if(overlay){
File[] files=srcFolder.listFiles();
int size=files.length;
for(int i=0;i<size;i++){
if(files[i].isFile()){
copyFileDifferentPath(files[i].getPath(),destFolder.getPath()+File.separator+files[i].getName(),true);
}else{
copyFolderDifferentPath(files[i].getPath(),destFolder.getPath()+File.separator+files[i].getName(),true);
}
}
}else{
System.out.println("文件夹重名");
return;
}
}else{
destFolder.mkdirs();
copyFolderDifferentPath(srcFolderPath,destFolderPath,true);
}
}
public static void copyFolderSamePath(String srcFolderPath,String destFolderPath){
File srcFolder=new File(srcFolderPath);
File destFolder=new File(destFolderPath);
if(!srcFolder.exists()){
System.out.println("源文件夹不存在");
return;
}
if(!(srcFolder.getPath().equals(destFolder.getPath()))){
System.out.println("方法调用错误:只复制同一个目录下的文件夹");
return;
}
if(!srcFolder.isDirectory()){
System.out.println("方法调用错误:源文件不是单个的文件夹");
return;
}
File newFolder=naming(destFolder);
newFolder.mkdirs();
File[] files=srcFolder.listFiles();
int size=files.length;
for(int i=0;i<size;i++){
if(files[i].isFile()){
copyFileDifferentPath(files[i].getPath(),newFolder.getPath()+File.separator+files[i].getName(),false);
}else{
copyFolderDifferentPath(files[i].getPath(),newFolder.getPath()+File.separator+files[i].getName(),false);
}
} }
/**
* 测试用main方法
* @param args
*/
public static void main(String[] args){
copyFileSamePath("F:\\World of Warcraft\\Data\\data\\data.019","F:\\World of Warcraft\\Data\\data\\data.019");
}
} //删除功能,配合上面的CopyFile可以达到剪切效果
package folderoperation; import java.io.File;
/**
* 注意它会删除文件,文件夹以及文件夹下的所有内容(根据指定的地址)
* @author Dawn
*
*/
public class DeleteFolder {
/**
* 删除文件的方法
* @param filePath 文件或文件夹的地址
* @return
*/
public static boolean delete(String filePath){
File file=new File(filePath);
if(file.exists()){
if(file.isFile()){
file.delete();
return true;
}else{
File[] files=file.listFiles();
int size=files.length;
for(int i=0;i<size;i++){
DeleteFolder.delete(files[i].getPath());
}
file.delete();
return true;
}
}else{
System.out.println("文件或文件夹不存在");
return false;
}
}
public static void main(String[] args){
delete("/Users/Dawn/Documents/JavaPractice/dataFile.txt");
}
}
修改后的CopyFile类的更多相关文章
- 将JAR包反编译,修改后重新打包(转)
将JAR包反编译,修改后重新打包(转) 在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的 ...
- P141 实战练习——字符串(修改后)
1.在项目中创建Number类,判断字符串“mingrikejijavabu”中字符‘i’出现了几次,并将结果输出. 方法一: // String str="mingrikejijavabu ...
- IDEA运行编译后配置文件无法找到,或配置文件修改后无效的问题
1.触发事件 今天正好在学习log4j,为了测试其配置文件log4j.properties中的各种配置,进行了频繁修改和程序启动以确认效果,因为是使用的IDEA建立的Web项目,接着问题就来了,配置文 ...
- java 记录对象前后修改的内容(工具类)
有时候业务需要,需记录一条记录的修改历史,但是不能为完成任务而硬编码,不靠谱 这种情况可以使用java反射来完成 对对象属性的描述可以通过自定义注解来完成,读取里面的属性进而记录修改历史. 在对象的属 ...
- eclipse 使用tomcat运行JavaWeb项目,文件修改后为何不用重启tomcat? (运行web项目的4种方式)探究
1.情景说明 在eclipse中,为什么Java文件修改后,重启tomcat class文件才能生效? 为什么jsp修改后,不需重启tomcat就能立即生效? 为什么静 ...
- spring boot注解 --@spring-boot-devtools 自动加载修改的文件和类
spriing boot中有一个注解,是自动加载修改后的类或者文件. 使用方法为: spring-boot-devtools=true 需要引入devtools包依赖: <dependency& ...
- static final修饰的静态变量修改后更新到服务器,重启无法生效的问题
今天在工作中碰到这样一个问题,有一个常量类,将工程中常用的一些变量定义在了里面.今天我要修改其中的某个变量.修改完后将编译好的.class文件更新到了服务器上,但是重启服务器后发现始终没有变化,还是以 ...
- Jar包进行反编译,修改后重新打包
在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的使用说明和源代码,有时有的却不提供源代码,使用 ...
- SpringBoot集成MybatisPlus解决Mapper文件修改后动态刷新的问题
很多人在使用SpringBoot集成Mybatis或者MybatisPlus的时候在查询复杂的情况下会写mapper文件,虽然说MyBatisPlus提供了常用的增删查改,但还是难以应付复杂的查询.关 ...
随机推荐
- info.plist、pch和四大对象(UIApplication、UIApplicationDelegate、UIWindow、UIViewController)
本文目录 1.程序配置文件info.plist,全局头文件pch 2.应用程序对象UIApplication介绍 3.UIApplicationDelegate介绍,程序启动过程 4.UIWindow ...
- doT.js详细介绍
doT.js详细介绍 doT.js特点是快,小,无依赖其他插件. 官网:http://olado.github.iodoT.js详细使用介绍 使用方法:{{= }} for interpolati ...
- 用Java实现简单的web服务器
运行结果: 1.WebServer.java文件 package webserver; import java.io.*; import java.net.*; public class WebSer ...
- 安卓初級教程(1):@Database(1)
package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...
- linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...
- 阿里云服务器Linux CentOS安装配置(零)目录
阿里云服务器Linux CentOS安装配置(零)目录 阿里云服务器Linux CentOS安装配置(一)购买阿里云服务器 阿里云服务器Linux CentOS安装配置(二)yum安装svn 阿里云服 ...
- (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.
想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...
- 【转】一个新的UIButtonMessage 给NGUI,使用委托,自动选择Receiver提供的方法
http://blog.csdn.net/chiuan/article/details/9290651?utm_source=tuicool&utm_medium=referral 来分享一个 ...
- windows 查看端口占用以及关闭该进程
win+r -> 输入cmd netstat -ano 找到占用端口进程的pid control+shift+esc 打开 任务管理器 menu->'查看'->'选择列->勾选 ...
- js获取上传文件个数 以及名称
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...