Java同步文件程序
来源:互联网 发布:哈尔滨智达网络科科技 编辑:程序博客网 时间:2024/05/21 21:20
Java同步文件程序
- package com.ycl.filter.FileCleaner;
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Reader;
- import java.io.Writer;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.io.IOUtils;
- public class FileSyn {
- static Map<String, String> map = new HashMap<String, String>();
- /**
- * 同步一个源文件目录到目标文件目录
- * 默认去.svn不进行同步
- * @param target
- * @param source
- * @param fileName
- */
- public static void synFile(String target, String source) {
- File sourcePath = new File(source);
- File targetPath = new File(target);
- index(sourcePath);
- synPath(sourcePath, targetPath,new FileFilter(){
- @Override
- public boolean accept(File file) {
- if(file.getAbsolutePath().contains(".svn")){
- return false;
- }
- return true;
- }
- });
- }
- /**
- * 同步一个源文件目录到目标文件目录
- *
- * @param target
- * @param source
- * @param fileName
- */
- public static void synFile(String target, String source,FileFilter fileFilter) {
- File sourcePath = new File(source);
- File targetPath = new File(target);
- index(sourcePath);
- synPath(sourcePath, targetPath,fileFilter);
- }
- /**
- * 同步两目录下的文件
- * 过滤.svn文件夹下的不需要同步
- * @param sourcePath
- * @param targetPath
- */
- public static void synPath(File sourcePath, File targetPath,FileFilter fileFilter) {
- if (sourcePath.isDirectory()) {
- File[] files = sourcePath.listFiles(fileFilter);
- for (File sourceFile : files) {
- try {
- if (sourceFile.isDirectory()) {
- File targetFile = new File(targetPath, sourceFile
- .getName());
- if (!targetFile.exists()) {// 目录不存在会自动创建
- targetFile.createNewFile();
- }
- synPath(sourceFile, targetFile,fileFilter);
- } else if (sourceFile.isFile()) {
- File targetFile = new File(targetPath, sourceFile
- .getName());
- if (!targetFile.exists()) {// 文件不存在会自动创建
- targetFile.createNewFile();
- }
- boolean flag = synFile(sourceFile, targetFile);
- if (flag) {
- System.out.println("同步文件:["
- + sourceFile.getAbsolutePath() + ","
- + targetFile.getAbsolutePath() + "]成功!");
- }
- }
- } catch (IOException e) {
- System.out.println("源文件:"+sourceFile.getAbsolutePath());
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 同步一个源文件目录到目标文件目录 指定某个文件名. 用源文件覆盖目录文件.[当源文件比目录文件新的时候].
- *
- * @param target
- * @param source
- * @param fileName
- */
- public static boolean synFile(File sourceFile, File targetFile) {
- boolean flag = false;
- Reader reader = null;
- Writer writer = null;
- try {
- if(sourceFile.getAbsolutePath().contains("AddRole")){
- System.out.println("source最后修改时间:"+sourceFile.lastModified());
- System.out.println("target最后修改时间:"+targetFile.lastModified());
- }
- if (sourceFile.lastModified() > targetFile.lastModified()) {
- reader = new FileReader(sourceFile);
- writer = new FileWriter(targetFile);
- IOUtils.copy(reader, writer);
- flag = true;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- flag = false;
- } catch (IOException e) {
- e.printStackTrace();
- flag = false;
- } finally {
- try {
- if (reader != null) {
- reader.close();
- }
- if (writer != null) {
- writer.close();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return flag;
- }
- /**
- * 创建目录索引 fileName ==> filePath
- *
- * @param sourcePath
- */
- private static void index(File sourcePath) {
- if (sourcePath.isDirectory()) {
- File[] files = sourcePath.listFiles();
- for (File file : files) {
- if (file.isDirectory()) {
- // map.put(file.h, file.get)
- }
- }
- }
- }
- }
这里本来想做索引的,后来想想就算了,同步整个目录得了,因为程序的速度太快了,我就不浪费程序代码了,代码多了本身就会崩溃。
测试代码如下:
- package com.ycl.filter.FileCleaner;
- import java.io.File;
- import java.io.FileFilter;
- /**
- * 这个同步挺管用的,不需要重启服务.不需要copy文件
- * @author yangchunlong.tw
- *
- */
- public class TestFileSyn {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //synConfigFile();1308563280000
- synClassFile();
- }
- /**
- * 同步配置文件
- * @param target
- * @param source
- */
- public static void synConfigFile(){
- String source = "D://workspace//branches//sportalModule//sportal-web//src//main//webapp//web";
- String target = "D://workspace//branches//sportalModule//target//sportal.war//web";
- FileSyn.synFile(target, source);
- }
- /**
- * 同步class文件,只同步class文件 这里的规则和同步配置文件一致,就是同步.
- * 设置同步的目录,以source为基本同步target目录文件
- */
- public static void synClassFile(){
- String source = "D://workspace//branches//sportalModule//sportal-web//target//classes";
- String target = "D://workspace//branches//sportalModule//target//sportal.war//WEB-INF//classes";
- FileSyn.synFile(target, source);
- }
- }
你可以测试修改配置文件,或者Java文件,借助eclipse的auto编译功能,可以copy 文件[class,xml...]到指定的目录,不需要重新maven打包部署,如果服务器支持动态加载,则不需要重启。
同步的测试结果代码如下:
- 同步文件:[D:/workspace/branches/sportalModule/sportal-web/target/classes/sql-map.xml,D:/workspace/branches/sportalModule/target/sportal.war/WEB-INF/classes/sql-map.xml]成功!
很清楚的表示,你修改的文件已经同步到目录目录了.
- Java同步文件程序
- java程序,线程同步
- Java多线程同步程序
- 文件同步程序,自动的文件复制
- Java文件同步软件(源代码)
- PHP编写文件多服务器同步程序
- Java中的同步——示例程序
- java文件拷贝程序
- java多线程网络传输文件(非同步)
- rsync+inotify文件变动后实时自动同步程序
- 同步定制 Unity团队 程序的C#文件模板
- 基于ftp实现一个类dropbox文件同步程序
- 文件同步
- 文件同步
- 文件同步
- java程序转成exe文件
- java程序转成exe文件
- Java程序打印excel文件
- C盘program files被隐藏
- 几个Java的开源爬虫
- 分析JAVA的Random类(1)
- 数据库查询慢的原因
- android ramdisk.img system.img userdata.img
- Java同步文件程序
- 回调函数 callback
- Gridview中添加Checkbox后,无法判断CheckBox的被选中状态?
- 闲着没事测了一下memcpy
- Java删除svn文件
- Java删除svn文件
- MySQL触发器
- 命名空间的使用方法
- 【编程语言】Android--模拟器里卸载应用程序的方法