1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Created by Administrator on 2017/6/12. 6 */ 7 public class PatchTool { 8 9 //1.读取配置文件 10 11 //2.在配置文件中配置本地文件路径前缀 12 13 //3.读取文件路径列表 14 15 //4.在输出文件夹生成相应的文件夹结构 16 17 public static final String CONFIG_PATH = "patch.properties"; 18 19 public static final String KEY_WORKSPACE_PATH = "WORKSPACE_PATH"; 20 21 public static final String KEY_OUTPUT_PATH = "OUTPUT_PATH"; 22 23 public static final String FILE_LIST_NAME = "list.txt";//存放修改过的文件的全路径列表文件 24 /** 25 * 1.读取配置文件 26 */ 27 public static Map<String,String> getProperties() { 28 Properties p = new Properties(); 29 Map<String,String> config = null; 30 try { 31 p.load(new BufferedInputStream(new FileInputStream(new File(CONFIG_PATH)))); 32 String workSpacePath = p.getProperty(KEY_WORKSPACE_PATH); 33 String outputPath = p.getProperty(KEY_OUTPUT_PATH); 34 config = new HashMap<>(); 35 config.put(KEY_OUTPUT_PATH,outputPath); 36 config.put(KEY_WORKSPACE_PATH,workSpacePath); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 return config; 41 } 42 43 // 3.读取文件路径列表 44 public static Set<String> getFilePaths(){ 45 Set<String> fileSets = null; 46 try { 47 BufferedReader br = new BufferedReader(new FileReader(new File(FILE_LIST_NAME))); 48 fileSets = new HashSet<>(); 49 String tmp; 50 while((tmp = br.readLine())!= null){ 51 fileSets.add(tmp); 52 } 53 br.close(); 54 } catch (FileNotFoundException e) { 55 e.printStackTrace(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 return fileSets; 60 } 61 62 // 4.根据文件路径生成文件夹结构 63 public static boolean createDir(Map<String,String> config,Set<String> fileSets){ 64 65 String workSpacePath = config.get(KEY_WORKSPACE_PATH); 66 String outputPath = config.get(KEY_OUTPUT_PATH); 67 68 String pathName,fileName,targetFile; 69 //遍历文件列表 70 for(String file : fileSets){ 71 pathName = outputPath + File.separator + file.substring(0,file.lastIndexOf(File.separator) + 1); 72 fileName = workSpacePath + File.separator + file; 73 targetFile = outputPath + File.separator + file; 74 75 File fileDir = new File(pathName); 76 File f; 77 if(!fileDir.exists()){ 78 fileDir.mkdirs(); 79 f = new File(targetFile); 80 if(!f.exists()){ 81 copyFile(fileName,targetFile); 82 } 83 } 84 85 } 86 87 88 return true; 89 } 90 91 public static void copyFile(String oldPath, String newPath) { 92 try { 93 int bytesum = 0; 94 int byteread = 0; 95 File oldfile = new File(oldPath); 96 if (oldfile.exists()) { //文件存在时 97 InputStream inStream = new FileInputStream(oldPath); //读入原文件 98 FileOutputStream fs = new FileOutputStream(newPath); 99 byte[] buffer = new byte[1444]; 100 int length; 101 while ( (byteread = inStream.read(buffer)) != -1) { 102 bytesum += byteread; //字节数 文件大小 103 System.out.println(bytesum); 104 fs.write(buffer, 0, byteread); 105 } 106 inStream.close(); 107 } 108 } 109 catch (Exception e) { 110 System.out.println("复制单个文件操作出错"); 111 e.printStackTrace(); 112 113 } 114 115 } 116 117 public static void main(String[] args) throws IOException{ 118 119 120 /* File f = new File("a.txt"); 121 f.createNewFile();*/ 122 123 Map<String,String> config = getProperties(); 124 Set<String> fileSets = getFilePaths(); 125 126 createDir(config,fileSets); 127 128 } 129 }
时间: 2024-10-05 23:46:55