?
class="java" name="code">package com.rd.svn;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
/**
*
* @author lh
*
*/
public class Main {
public static void main(String[] args) throws SVNException {
System.out.println("===================== corp start =====================");
String url = "http://xxx.com/branches/br_yyyy_v3.1.0_20170523";
String copeDir = "D:/workspaces/p2pv3/v3.1.0";
String targetDir = "f:/v3.1.0";
String username = "zhangsan";
String password = "zhangsan";
String prefixDir = "/branches/br_yyyy_v3.1.0_20170523";
List<String> pathList = new ArrayList<>();
List<SVNLogEntry> logs = new SVNLog(url, username, password).getSVNLogs(6387,6402);//5837
for (SVNLogEntry log : logs) {
if (log.getAuthor().equals("xyadmin")) {
continue;
}
System.out.println(log.getAuthor()+ ", 版本号:"+log.getRevision()+", 日期:"+ format(log.getDate()));
Map<String, SVNLogEntryPath> paths = log.getChangedPaths();
Iterator<String> its = paths.keySet().iterator();
while (its.hasNext()) {
String key = its.next();
String path = paths.get(key).getPath();
if(path.startsWith(prefixDir) && path.indexOf(".") !=-1){
pathList.add(path);
}
}
}
SVNLogEntry lastLog = logs.get(logs.size() - 1);
System.out.println("文件数:"+pathList.size()+", 版本号:"+lastLog.getRevision()+", 日期:"+ format(lastLog.getDate()));
for (String path : pathList) {
path = path.replace(prefixDir, "");
String srcPath = copeDir + path;
String destDir = targetDir +path.substring(0, path.lastIndexOf("/"));
FileUtils.copyGeneralFile(srcPath, destDir);
}
System.out.println("===================== corp over =====================");
}
private static String format(Date date){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
}
?
package com.rd.svn; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.util.Properties; /* * Java实现文件复制、剪切、caozuo.html" target="_blank">删除操作 * 文件指文件或文件夹 * 文件分割符统一用"//" */ public class FileUtils { /** * 复制文件或文件夹 * * @param srcPath * @param destDir * 目标文件所在的目录 * @return */ public static boolean copyGeneralFile(String srcPath, String destDir) { boolean flag = false; File file = new File(srcPath); if (!file.exists()) { return false; } if (file.isFile()) { // 源文件 flag = copyFile(srcPath, destDir); } else if (file.isDirectory()) { //System.out.println(srcPath+":"+destDir); //flag = copyDirectory(srcPath, destDir); } return flag; } /** * 复制文件 * * @param srcPath * 源文件绝对路径 * @param destDir * 目标文件所在目录 * @return boolean */ private static boolean copyFile(String srcPath, String destDir) { boolean flag = false; File srcFile = new File(srcPath); if (!srcFile.exists()) { // 源文件不存在 //System.out.println("源文件不存在"); return false; } // 获取待复制文件的文件名 String fileName = srcPath .substring(srcPath.lastIndexOf("/")); String destPath = destDir + fileName; if (destPath.equals(srcPath)) { // 源文件路径和目标文件路径重复 //System.out.println("源文件路径和目标文件路径重复!"); return false; } File destFile = new File(destPath); if (destFile.exists() && destFile.isFile()) { // 该路径下已经有一个同名文件 //System.out.println("目标目录下已有同名文件!"); destFile.delete(); } File destFileDir = new File(destDir); destFileDir.mkdirs(); try { FileInputStream fis = new FileInputStream(srcPath); FileOutputStream fos = new FileOutputStream(destFile); byte[] buf = new byte[1024]; int c; while ((c = fis.read(buf)) != -1) { fos.write(buf, 0, c); } fis.close(); fos.close(); flag = true; } catch (IOException e) { e.printStackTrace(); } if (flag) { //System.out.println("复制文件成功!"); } return flag; } /** * * @param srcPath * 源文件夹路径 * @param destPath * 目标文件夹所在目录 * @return */ private static boolean copyDirectory(String srcPath, String destDir) { //System.out.println("复制文件夹开始!"); boolean flag = false; File srcFile = new File(srcPath); if (!srcFile.exists()) { // 源文件夹不存在 //System.out.println("源文件夹不存在"); return false; } // 获得待复制的文件夹的名字,比如待复制的文件夹为"E://dir"则获取的名字为"dir" String dirName = getDirName(srcPath); // 目标文件夹的完整路径 String destPath = destDir + File.separator + dirName; // //System.out.println("目标文件夹的完整路径为:" + destPath); if (destPath.equals(srcPath)) { //System.out.println("目标文件夹与源文件夹重复"); return false; } File destDirFile = new File(destPath); if (destDirFile.exists()) { // 目标位置有一个同名文件夹 //System.out.println("目标位置已有同名文件夹!"); return false; } destDirFile.mkdirs(); // 生成目录 File[] fileList = srcFile.listFiles(); // 获取源文件夹下的子文件和子文件夹 if (fileList.length == 0) { // 如果源文件夹为空目录则直接设置flag为true,这一步非常隐蔽,debug了很久 flag = true; } else { for (File temp : fileList) { if (temp.isFile()) { // 文件 flag = copyFile(temp.getAbsolutePath(), destPath); } else if (temp.isDirectory()) { // 文件夹 flag = copyDirectory(temp.getAbsolutePath(), destPath); } if (!flag) { break; } } } if (flag) { //System.out.println("复制文件夹成功!"); } return flag; } /** * 获取待复制文件夹的文件夹名 * * @param dir * @return String */ private static String getDirName(String dir) { if (dir.endsWith(File.separator)) { // 如果文件夹路径以"//"结尾,则先去除末尾的"//" dir = dir.substring(0, dir.lastIndexOf(File.separator)); } return dir.substring(dir.lastIndexOf(File.separator) + 1); } /** * 删除文件或文件夹 * * @param path * 待删除的文件的绝对路径 * @return boolean */ public static boolean deleteGeneralFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { // 文件不存在 //System.out.println("要删除的文件不存在!"); } if (file.isDirectory()) { // 如果是目录,则单独处理 flag = deleteDirectory(file.getAbsolutePath()); } else if (file.isFile()) { flag = deleteFile(file); } if (flag) { //System.out.println("删除文件或文件夹成功!"); } return flag; } /** * 删除文件 * * @param file * @return boolean */ private static boolean deleteFile(File file) { return file.delete(); } /** * 删除目录及其下面的所有子文件和子文件夹,注意一个目录下如果还有其他文件或文件夹 * 则直接调用delete方法是不行的,必须待其子文件和子文件夹完全删除了才能够调用delete * * @param path * path为该目录的路径 */ private static boolean deleteDirectory(String path) { boolean flag = true; File dirFile = new File(path); if (!dirFile.isDirectory()) { return flag; } File[] files = dirFile.listFiles(); for (File file : files) { // 删除该文件夹下的文件和文件夹 // Delete file. if (file.isFile()) { flag = deleteFile(file); } else if (file.isDirectory()) {// Delete folder flag = deleteDirectory(file.getAbsolutePath()); } if (!flag) { // 只要有一个失败就立刻不再继续 break; } } flag = dirFile.delete(); // 删除空目录 return flag; } /** * 由上面方法延伸出剪切方法:复制+删除 * * @param destDir * 同上 */ public static boolean cutGeneralFile(String srcPath, String destDir) { if (!copyGeneralFile(srcPath, destDir)) { //System.out.println("复制失败导致剪切失败!"); return false; } if (!deleteGeneralFile(srcPath)) { //System.out.println("删除源文件(文件夹)失败导致剪切失败!"); return false; } //System.out.println("剪切成功!"); return true; } public static void main(String[] args) throws IOException { //copyGeneralFile("E:\\test.txt", "E:\\New.txt"); // 复制文件 // copyGeneralFile("E://hello", "E://world"); // 复制文件夹 // deleteGeneralFile("E://onlinestockdb.sql"); // 删除文件 // deleteGeneralFile("E://woman"); // 删除文件夹 //cutGeneralFile("E://hello", "E://world"); // 剪切文件夹 //cutGeneralFile("E:\\test.txt", "E:\\Cow"); // 剪切文件 InputStream in = ClassLoader.getSystemResourceAsStream("care.properties"); URL url = ClassLoader.getSystemResource("care.properties"); //System.out.println(url.getPath()); //System.out.println(url.toString()); Properties p = new Properties(); p.setProperty("lastTime", "2016-5-27 9:15:56"); OutputStream os = new FileOutputStream(url.getPath()); p.store(os, "little"); } }
?
package com.rd.svn;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
/**
* 日志查询类
* @author lwp
*
*/
public class SVNLog {
private String url;
private String username;
private String password;
private SVNRepository repository;
public SVNLog(String url, String username, String password)
throws SVNException {
this.url = url;
this.username = username;
this.password = password;
// 版本库初始化
DAVRepositoryFactory.setup();
repository = DAVRepositoryFactory.create(SVNURL.parseURIEncoded(url));
ISVNAuthenticationManager authManager = SVNWCUtil
.createDefaultAuthenticationManager(username, password);
repository.setAuthenticationManager(authManager);
}
public List<SVNLogEntry> getSVNLogs(long startRevision, long endRevision) {
try {
// 存放结果
List<SVNLogEntry> logEntries = new ArrayList<SVNLogEntry>();
repository.log(new String[] { "/" }, logEntries, startRevision,
endRevision, true, true);
return logEntries;
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public List<SVNLogEntry> getSVNLogs(long startRevision) {
long lastRevision = 0;
try {
lastRevision = repository.getLatestRevision();
return getSVNLogs(startRevision,lastRevision);
} catch (SVNException e) {
e.printStackTrace();
return null;
}
}
//获取今日提交记录
public List<SVNLogEntry> getSVNLogs() {
long lastRevision = 0;
long startRevision=0;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date today = format.parse(format.format(new Date(new Date().getTime()-24*60*60*1000)));
startRevision = repository.getDatedRevision(today);
//lastRevision = repository.getLatestRevision();
return getSVNLogs(startRevision,-1);
} catch (SVNException e) {
e.printStackTrace();
return null;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws SVNException {
String url = "svn://XX";
String username = "XX";
String password = "XX";
List<SVNLogEntry> logs = new SVNLog(url, username, password)
.getSVNLogs();
for (SVNLogEntry log : logs) {
System.out.println(log.getDate());
}
}
}
?