背景:

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

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

代码如下:

 public class PropertiesCleaner {
private static final String PROPERTIE_FILE = "D:\\Workspaces\\WebRoot\\WEB-INF\\classes\\lang_zh_TW.properties";
private static final String SRC_FOLDER = "D:\\Workspaces\\WebRoot"; private ArrayList<String> propertiesKeys = new ArrayList<String>();
private ArrayList<File> sourceFiles = new ArrayList<File>();
private ArrayList<String> deleteKeys = new ArrayList<String>(); private static boolean isShowUsing = false; /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new PropertiesCleaner().run();
} public void run() {
try {
loadAllProperties(PROPERTIE_FILE); getAllFiles(new File(SRC_FOLDER));
System.out.println("Get source files count: " + sourceFiles.size()); searchFilesForProperties(); deleteProperties();
}catch (Exception e) {
e.printStackTrace();
}
} private void loadAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
Enumeration en = pps.propertyNames(); while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
propertiesKeys.add(strKey);
//System.out.println(strKey);
//String strValue = pps.getProperty(strKey);
//System.out.println(strKey + "=" + strValue);
}
System.out.println("Get properties key count: " + propertiesKeys.size()); } private void getAllFiles(File file){
File[] fs = file.listFiles(new FileFilter() { @Override
public boolean accept(File pathname) {
if(pathname.isDirectory())
return true;
String name = pathname.getName();
return name.endsWith(".java") || name.endsWith(".html") || name.endsWith(".js");
}
});
for(File f:fs){
if(f.isDirectory())
getAllFiles(f);
if(f.isFile()) {
sourceFiles.add(f);
//System.out.println(f);
}
}
} private void searchFilesForProperties() {
int usedTimes = 0;
int notUsedTimes = 0;
for (String keyStr : propertiesKeys) {
boolean isUsing = false;
for (File file : sourceFiles) {
isUsing = isUsing || searchFileForKey(file, keyStr);
}
if(!isUsing) {
//System.out.println("Properties " + keyStr + " is not used anymore.");
notUsedTimes ++;
deleteKeys.add(keyStr);
} else {
usedTimes ++;
}
}
if (isShowUsing) {
System.out.println(usedTimes + " properties are using.");
}
System.out.println(notUsedTimes + " properties not used anymore.");
} private boolean searchFileForKey(File file, String keyword){
LineNumberReader reader = null;
int times = 0;
try {
reader = new LineNumberReader(new FileReader(file));
String readLine = null;
ArrayList<String> lines = new ArrayList<String>();
while((readLine = reader.readLine()) != null) {
// int index = 0;
// int next = 0;
/*
while((index = readLine.indexOf(keyword,next)) != -1) {
next = index + keyword.length();
times++;
}
if(times > 0) {
System.out.println("No." + reader.getLineNumber() + " line: has " + times + " times");
}
*/
if(readLine.indexOf(keyword) != -1) {
times++;
lines.add(reader.getLineNumber() + ": " + readLine);
}
}
if(times > 0) {
if (isShowUsing) {
System.out.println("Found " + keyword + " " + times + " in file " + file.getName());
for (String str : lines) {
System.out.println(" " + str);
}
System.out.println();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return times > 0;
} private void deleteProperties() throws IOException, FileNotFoundException {
if(deleteKeys.size() == 0)
return; LineNumberReader reader = null;
BufferedWriter bw = null;
int times = 0;
try {
reader = new LineNumberReader(new FileReader(PROPERTIE_FILE));
String readLine = null;
ArrayList<String> lines = new ArrayList<String>();
bw = new BufferedWriter(new FileWriter(PROPERTIE_FILE+"_1"));
while((readLine = reader.readLine()) != null) {
boolean keyInLine = false;
for (String keyword : deleteKeys) {
keyInLine = keyInLine || readLine.indexOf(keyword) != -1;
}
if(!keyInLine) {
lines.add(readLine);
bw.write(readLine);
bw.newLine();
}
}
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
if (bw != null)
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
deleteProperties()这个,本来想使用Properties来做删除。网上也查到了别人写的很不错的例子,还包含了encode这些。但用的时候发现,因为我的Properties文件里
全是unicode转码过的值,encode的时候会比较麻烦。干脆直接按行直接做删除处理。性能可能一般,但是好在思路直接。

检索源码 删除无用Properties的小工具的更多相关文章

  1. Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)

                                                                                                        ...

  2. docker 删除无用的镜像文件的命令小计

    df -h  查看当前服务器的内存情况 docker system prune  删除无用镜像文件命令 执行ok之后,再次查看内存情况.

  3. 用C#Winform写个简单的批量清空文件内容和删除文件的小工具

    用C#Winform写个简单的批量清空文件内容和删除文件的小工具 本文介绍这个简单得不能再简单的小项目.做这个项目,有以下目的. 1 当然是做个能用的工具 2 学习使用Github 关于用VS2013 ...

  4. iOS ipa包瘦身------删除无用图片资源

         随着客户端业务的增多和业务的更新,App包大小越来越大,优化包大小是迫在眉睫,客户端需要优化的地方也有很多,本期主要讲如何查找无用图片并且删除无用图片的方法.      方案1:(暴力方法) ...

  5. ueditor上传图片配置成功,但是如何删除无用的图片

    我使用ueditor作为富文本编辑器,配置已经好了,上传功能也好了.现在的问题是当使用ueditor上传图片的时候,选择了图片就立刻上传到指定的文件夹里,而后续即使没有保存该篇文章内容,即取消操作,图 ...

  6. 直播平台源码搭建教程:微信小程序中的直播如何去掉水印

    直播平台源码搭建教程:微信小程序中的直播如何去掉水印 本文与大家分享一下直播平台源码搭建教程,如何去掉直播视频的水印 var services = require('../../lib/service ...

  7. Oracle监控用户索引使用情况,删除无用索引

    监控当前业务用户索引 一段时间后查询从未被使用的索引,删除无用索引 停止监控索引 1. 监控当前用户所有索引 得到监控所有索引的语句: select 'alter index ' || index_n ...

  8. android删除无用资源文件的python脚本

    随着android项目的进行,如果没有及时删除无用的资源时安装包会越来越大,是时候整理一下废弃资源缩小压缩包了,少年! 其实判断一个资源(drawable,layout)是否没有被使用很简单,文件名( ...

  9. Android lint 删除无用图片文件和配置文件

    Android lint  删除无用.冗余的  配置文件和 图片资源    转载请注明  http://blog.csdn.net/aaawqqq?viewmode=contents Android项 ...

随机推荐

  1. SQLSERVER2017 链接数据库不行

    以为作业要把mysql 数据导入sqlserver,自己装了sqlserver2017, 通过ODBC 来先创建ODBC,mysql的数据库然后测试成功后,在sqlserver创建链接数据库,一直有问 ...

  2. 61.index CUD

    主要知识点 索引CUD     一.创建索引的语法     PUT /my_index { "settings": { ... any settings ... }, " ...

  3. 使用MySQL Migration Toolkit快速导入Oracle数据

    近来笔者有项目需要将原有的Oracle数据库中的数据导入到MySQL中,经过试用发现MySQL GUI Tools中的MySQL Migration Toolkit可以非常方便快捷的将Oracle数据 ...

  4. nginx 、tomcat 集群配置、shiro Session 共享

    一.nginx.config 配置 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error. ...

  5. windows终端 进入文件夹

    盘符: 例如想进入D盘 d: cd 进入到当前盘某个目录.cd \ 进入当前盘根目录cd \windows 进入到当前盘Windows目录cd.. 退出到上一级目录 注:进入含有特殊字符目录时需要加引 ...

  6. Java内联函数

    1.内联函数就是指函数在被调用的地方直接展开,编译器在调用时不用像一般函数那样,參数压栈,返回时參数出栈以及资源释放等,这样提高了程序运行速度. 2.Java语言中有一个keywordfinal来指明 ...

  7. libev与libuv的区别

    参考: http://blog.csdn.net/w616589292/article/details/46475555 libuv 和 libev ,两个名字相当相近的 I/O Library,最近 ...

  8. ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)

    题目链接:problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Do you ...

  9. instanceof运算符的使用

    在之前的学习中,经常遇到instanceof运算符,对于它的用法总感觉理解不到位,所以专门总结一下它的用法加深理解. instanceof主要用来判断一个类是否实现了某个接口,或者判断一个实例对象是否 ...

  10. How do you install Google Chrome on Ubuntu?

    https://askubuntu.com/questions/510056/how-to-install-google-chrome sudo apt-get install chromium-br ...