检索源码 删除无用Properties的小工具
背景:
重新做项目的过程中,引用了大量旧代码。尤其是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的小工具的更多相关文章
- Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)
...
- docker 删除无用的镜像文件的命令小计
df -h 查看当前服务器的内存情况 docker system prune 删除无用镜像文件命令 执行ok之后,再次查看内存情况.
- 用C#Winform写个简单的批量清空文件内容和删除文件的小工具
用C#Winform写个简单的批量清空文件内容和删除文件的小工具 本文介绍这个简单得不能再简单的小项目.做这个项目,有以下目的. 1 当然是做个能用的工具 2 学习使用Github 关于用VS2013 ...
- iOS ipa包瘦身------删除无用图片资源
随着客户端业务的增多和业务的更新,App包大小越来越大,优化包大小是迫在眉睫,客户端需要优化的地方也有很多,本期主要讲如何查找无用图片并且删除无用图片的方法. 方案1:(暴力方法) ...
- ueditor上传图片配置成功,但是如何删除无用的图片
我使用ueditor作为富文本编辑器,配置已经好了,上传功能也好了.现在的问题是当使用ueditor上传图片的时候,选择了图片就立刻上传到指定的文件夹里,而后续即使没有保存该篇文章内容,即取消操作,图 ...
- 直播平台源码搭建教程:微信小程序中的直播如何去掉水印
直播平台源码搭建教程:微信小程序中的直播如何去掉水印 本文与大家分享一下直播平台源码搭建教程,如何去掉直播视频的水印 var services = require('../../lib/service ...
- Oracle监控用户索引使用情况,删除无用索引
监控当前业务用户索引 一段时间后查询从未被使用的索引,删除无用索引 停止监控索引 1. 监控当前用户所有索引 得到监控所有索引的语句: select 'alter index ' || index_n ...
- android删除无用资源文件的python脚本
随着android项目的进行,如果没有及时删除无用的资源时安装包会越来越大,是时候整理一下废弃资源缩小压缩包了,少年! 其实判断一个资源(drawable,layout)是否没有被使用很简单,文件名( ...
- Android lint 删除无用图片文件和配置文件
Android lint 删除无用.冗余的 配置文件和 图片资源 转载请注明 http://blog.csdn.net/aaawqqq?viewmode=contents Android项 ...
随机推荐
- webpack学习笔记(1)--webpack.config.js
主要的信息都是来自于下方所示的网站 https://webpack.docschina.org/configuration 从 webpack 4.0.0 版本开始,可以不用通过引入一个配置文件打包项 ...
- nyoj51-管闲事的小明
管闲事的小明 时间限制:4000 ms | 内存限制:65535 KB 难度:2 描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端 ...
- 2019-04-02 cast and covert
convert 专用于SQLServer,cast对于其它数据库的兼容性更好 convert 处理日期和时间值更厉害 语法不一样: cast(itemvalue as decimal(19,6)) c ...
- springcloud 中文文档
spring cloud 中文文档:https://springcloud.cc/spring-cloud-dalston.html spring cloud 中文网:https://springcl ...
- (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...
- [bzoj1369][Baltic2003]Gem_树形dp_结论题
Gem bzoj-1369 Baltic-2003 题目大意:给你一棵树,让你往节点上添自然数,使得任意相邻节点的数不同且使得权值最小. 注释:n为结点个数,$1\le n\le 10^3$. 想法: ...
- android 软键盘的显示与隐藏问题的研究
在android中,常常会和输入法的软件键盘交互.在Manifest文件中,系统给activity的一个属性-windowSoftInputMode来控制输入法的显示方式. 该属性提供了Activit ...
- 【MVC框架】——什么是MVC框架
学习了经典三层之后,认为不论什么一种框架都不再是难的.不管如何,都须要连接数据库.业务逻辑处理.显示.其余的无非就是给三层解耦合.解耦合越到位,这个框架就越easy被接受. 百度百科:MVC全名是Mo ...
- Map 遍历取值及jstl的取值
Map 遍历取值及jstl的取值 学习了:http://blog.csdn.net/yanjiaye520/article/details/17354239 1.Java map的便利取值 Java代 ...
- POJ 题目3020 Antenna Placement(二分图)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7011 Accepted: 3478 ...