java复制文件,将一些文件复制到新的工作空间
来源:互联网 发布:算法工程师考试题目 编辑:程序博客网 时间:2024/04/29 07:13
java复制文件,
使用环境:
有时候svn分支变换很快,这时原来workspace中的一些配置信息测试类需要手工复制到新的工作空间中,而有了这个类,只需要把修改过的文件移到目录下,运行程序就行了,不用在手工拷贝代码。目录位置根据需要修改,项目名称根据需要修改
import java.io.BufferedReader;
import java.io.File;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* @date 2016年10月31日 上午9:26:19
*/
public class MvToNewWorkspace {
public final String CORE = "core\\";
final String targetWorkPath = "workspace65test";
String targetPath = "D:\\tools\\" + targetWorkPath + "\\";
final String srcPath = "C:\\Users\\Administrator\\Desktop\\工作\\新workspace调整\\";
final boolean OverLay = true;
final String javaFolder = "src\\";
final String resFolder = "resources\\";
final String webxmlFolder = "WebContent\\WEB-INF\\";
final String resFolderCore="src\\main\\resources\\";
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("start");
new MvToNewWorkspace().start();
System.out.println("end");
}
public void start() {
mv(srcPath , CORE);
}
public void mv(String waitedMvFolder,String subFolder) {
File srcDir = new File(waitedMvFolder+subFolder);
File[] files = srcDir.listFiles();
if (files == null || files.length == 0) {
return;
}
for (File file : files) {
if (file.getName().endsWith(".java")) {
copy(file.getAbsolutePath(), targetPath + subFolder+javaFolder + getPackage(file)+"\\"+file.getName(), OverLay);
} else if (file.getName().endsWith(".properties")) {
if (subFolder.equals(CORE)) {//src\main\resources
copy(file.getAbsolutePath(), targetPath +subFolder+ resFolderCore + file.getName(), OverLay);
}else {
copy(file.getAbsolutePath(), targetPath +subFolder+ resFolder + file.getName(), OverLay);
}
} else if (file.getName().equals("web.xml")) {
copy(file.getAbsolutePath(), targetPath + subFolder+webxmlFolder +file.getName(), OverLay);
}
}
}
public String getPackage(File file) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// System.out.println("line " + line + ": " + tempString);
if (tempString.contains("package")) {
tempString = tempString.replaceAll("[\\s;]|package", "");
tempString = tempString.replace(".", "\\");
return tempString;
}
line++;
if (line > 20) {
return "";
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return "";
}
public static boolean copy(String srcFileName, String destFileName, boolean overlay) {
File srcFile = new File(srcFileName);
String message= "";
// 判断源文件是否存在
if (!srcFile.exists()) {
message= "源文件:" + srcFileName + "不存在!";
System.out.println(message);
return false;
} else if (!srcFile.isFile()) {
message= "复制文件失败,源文件:" + srcFileName + "不是一个文件!";
System.out.println(message);
return false;
}
// 判断目标文件是否存在
File destFile = new File(destFileName);
if (destFile.exists()) {
// 如果目标文件存在并允许覆盖
if (overlay) {
// 删除已经存在的目标文件,无论目标文件是目录还是单个文件
new File(destFileName).delete();
}
} else {
// 如果目标文件所在目录不存在,则创建目录
if (!destFile.getParentFile().exists()) {
// 目标文件所在目录不存在
if (!destFile.getParentFile().mkdirs()) {
// 复制文件失败:创建目标文件所在目录失败
return false;
}
}
}
copy(srcFile, destFile);
return true;
}
public static boolean copy(File srcFile, File destFile) {
FileChannel fileChannelIn = null;
FileChannel fileChannelOut = null;
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFile);
fileOutputStream = new FileOutputStream(destFile);
fileChannelIn = fileInputStream.getChannel();
fileChannelOut = fileOutputStream.getChannel();
fileChannelIn.transferTo(0, fileChannelIn.size(), fileChannelOut);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
fileChannelOut.close();
fileChannelIn.close();
fileOutputStream.close();
fileInputStream.close();
} catch (IOException e) {
}
}
return true;
}
}
0 0
- java复制文件,将一些文件复制到新的工作空间
- Java将文件复制到剪切板
- java 将远程共享的文件复制到本地
- 将主机的文件复制到虚拟机
- 将文件复制到临时文件夹
- 如何将Eclipse的一个工作空间的配置文件复制到另一个工作空间
- 如何将Eclipse的一个工作空间的配置文件复制到另一个工作空间
- 如何将Myeclipse的一个工作空间的配置文件复制到另一个工作空间
- 将'文件1'的内容复制到'文件2'中
- java 将某一文件夹下的所有文件 复制到指定文件下
- JAVA 文件的复制
- java 文件的复制
- java:文件的复制
- 文件复制的一些资料
- 将 CAB 文件复制到模拟器
- Android 将文件复制到指定目录
- 将文件复制到分页程序
- 将桌面文件复制到D盘
- [iOS]调用系统电话,邮件,手机QQ,微信
- Android RecyclerView 中的 item 如何居中
- The database profile could not be loaded. Check log for details
- 由A*的学习引发对bfs, dijkstra的思考
- Dynamics CRM build numbers
- java复制文件,将一些文件复制到新的工作空间
- 三星官方uboot移植实战2
- git学习笔记
- oracle 查询最近执行过的 SQL语句
- WebService
- ssh只使用密码登录
- python 动态数组
- 基于session.upload_progress 的文件上传进度显示
- 前端资源汇集