检索源码 删除无用Properties的小工具

Posted tianxindaoren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检索源码 删除无用Properties的小工具相关的知识,希望对你有一定的参考价值。

背景:

重新做项目的过程中,引用了大量旧代码。尤其是Properties文件,里面肯定有一批是无用的,干脆笨办法直接扫描源码文件来过滤。

后续在此基础上修改修改,再做个扫描无用image文件的类。

 

代码如下:

  1 public class PropertiesCleaner {
  2     private static final String PROPERTIE_FILE = "D:\Workspaces\WebRoot\WEB-INF\classes\lang_zh_TW.properties";
  3     private static final String SRC_FOLDER = "D:\Workspaces\WebRoot";
  4     
  5     private ArrayList<String> propertiesKeys = new ArrayList<String>();
  6     private ArrayList<File> sourceFiles = new ArrayList<File>();
  7     private ArrayList<String> deleteKeys = new ArrayList<String>();
  8     
  9     private static boolean isShowUsing = false;
 10     
 11     
 12     /**
 13      * @param args
 14      */
 15     public static void main(String[] args) {
 16         // TODO Auto-generated method stub
 17         new PropertiesCleaner().run();
 18     }
 19     
 20     public void run() {
 21         try {
 22             loadAllProperties(PROPERTIE_FILE);
 23 
 24             getAllFiles(new File(SRC_FOLDER));
 25             System.out.println("Get source files count: " + sourceFiles.size());
 26             
 27             searchFilesForProperties();
 28             
 29             deleteProperties();
 30         }catch (Exception e) {
 31             e.printStackTrace();
 32         }
 33     }
 34     
 35     private void loadAllProperties(String filePath) throws IOException {
 36         Properties pps = new Properties();
 37         InputStream in = new BufferedInputStream(new FileInputStream(filePath));
 38         pps.load(in);
 39         Enumeration en = pps.propertyNames(); 
 40         
 41         while(en.hasMoreElements()) {
 42             String strKey = (String) en.nextElement();
 43             propertiesKeys.add(strKey);
 44             //System.out.println(strKey);
 45             //String strValue = pps.getProperty(strKey);
 46             //System.out.println(strKey + "=" + strValue);
 47         }
 48         System.out.println("Get properties key count: " + propertiesKeys.size());
 49         
 50     }
 51     
 52     private void getAllFiles(File file){
 53         File[] fs = file.listFiles(new FileFilter() {
 54             
 55             @Override
 56             public boolean accept(File pathname) {
 57                 if(pathname.isDirectory())
 58                     return true;
 59                 String name = pathname.getName();
 60                 return name.endsWith(".java") || name.endsWith(".html") || name.endsWith(".js");
 61             }
 62         });
 63         for(File f:fs){
 64             if(f.isDirectory()) 
 65                 getAllFiles(f);
 66             if(f.isFile()) {
 67                 sourceFiles.add(f);
 68                 //System.out.println(f);
 69             }
 70         }
 71     }
 72     
 73     private void searchFilesForProperties() {
 74         int usedTimes = 0;
 75         int notUsedTimes = 0;
 76         for (String keyStr : propertiesKeys) {
 77             boolean isUsing = false;
 78             for (File file : sourceFiles) {
 79                 isUsing = isUsing || searchFileForKey(file, keyStr);
 80             }
 81             if(!isUsing) {
 82                 //System.out.println("Properties " + keyStr + " is not used anymore.");
 83                 notUsedTimes ++;
 84                 deleteKeys.add(keyStr);
 85             } else {
 86                 usedTimes ++;
 87             }
 88         }
 89         if (isShowUsing) {
 90             System.out.println(usedTimes + " properties are using.");
 91         }
 92         System.out.println(notUsedTimes + " properties not used anymore.");
 93     }
 94 
 95     private boolean searchFileForKey(File file, String keyword){
 96         LineNumberReader reader = null;
 97         int times = 0;
 98         try {
 99             reader = new LineNumberReader(new FileReader(file));
100             String readLine = null;
101             ArrayList<String> lines = new ArrayList<String>();
102             while((readLine = reader.readLine()) != null) {
103 //                int index = 0;
104 //                int next = 0;
105                 /*
106                 while((index = readLine.indexOf(keyword,next)) != -1) {
107                     next = index + keyword.length();
108                     times++;
109                 }
110                 if(times > 0) {
111                     System.out.println("No." + reader.getLineNumber() + " line: has " + times + " times");
112                 }
113                 */
114                 if(readLine.indexOf(keyword) != -1) {
115                     times++;
116                     lines.add(reader.getLineNumber() + ": " + readLine);
117                 }
118             }
119             if(times > 0) {
120                 if (isShowUsing) {
121                     System.out.println("Found " + keyword + " " + times + " in file " + file.getName());
122                     for (String str : lines) {
123                         System.out.println("    " + str);
124                     }
125                     System.out.println();
126                 }
127             }
128         } catch (IOException e) {
129             // TODO Auto-generated catch block
130             e.printStackTrace();
131         } finally {
132             try {
133                 if (reader != null)
134                     reader.close();
135             } catch (IOException e) {
136                 // TODO Auto-generated catch block
137                 e.printStackTrace();
138             }
139         }
140         return times > 0;
141     }
142     
143     private void deleteProperties() throws IOException, FileNotFoundException {
144         if(deleteKeys.size() == 0)
145             return;
146         
147         LineNumberReader reader = null;
148         BufferedWriter bw = null;
149         int times = 0;
150         try {
151             reader = new LineNumberReader(new FileReader(PROPERTIE_FILE));
152             String readLine = null;
153             ArrayList<String> lines = new ArrayList<String>();
154             bw = new BufferedWriter(new FileWriter(PROPERTIE_FILE+"_1"));
155             while((readLine = reader.readLine()) != null) {
156                 boolean keyInLine = false;
157                 for (String keyword : deleteKeys) {
158                     keyInLine = keyInLine || readLine.indexOf(keyword) != -1;
159                 }
160                 if(!keyInLine) {
161                     lines.add(readLine);
162                     bw.write(readLine);
163                     bw.newLine();
164                 }
165             }
166             bw.flush();
167         } catch (IOException e) {
168             // TODO Auto-generated catch block
169             e.printStackTrace();
170         } finally {
171             try {
172                 if (reader != null)
173                     reader.close();
174                 if (bw != null)
175                     bw.close();
176             } catch (IOException e) {
177                 // TODO Auto-generated catch block
178                 e.printStackTrace();
179             }
180         }
181     }
182 }

 

deleteProperties()这个,本来想使用Properties来做删除。网上也查到了别人写的很不错的例子,还包含了encode这些。但用的时候发现,因为我的Properties文件里
全是unicode转码过的值,encode的时候会比较麻烦。干脆直接按行直接做删除处理。性能可能一般,但是好在思路直接。

以上是关于检索源码 删除无用Properties的小工具的主要内容,如果未能解决你的问题,请参考以下文章

Properties文件工具类的使用--获取所有的键值删除键更新键等操作

重要文件被误删如何恢复?找回数据的小技巧

Mybatis 实现批量插入和批量删除源码实例

JAVA无用代码检查工具:UCDelete

不编程不写代码,微信小程序视频教程,开发工具+源码+后台

Lucene快速入门——索引的创建,检索,更新和删除