检索源码 删除无用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项 ...
随机推荐
- CSS font-style中italic和Oblique有何区别 标签: css字体 2017-01-05 14:42 60人阅读 评论
*要搞清楚这个问题,首先要明白字体是怎么回事.一种字体有粗体.斜体.下划线.删除线等诸多属性. 但是并不是所有字体都做了这些,一些不常用的字体,或许就只有个正常体,如果你用Italic,就没有效果了~ ...
- PHP socket初探 --- 关于IO的一些枯燥理论
[原文地址:https://blog.ti-node.com/blog...] 要想更好了解socket编程,有一个不可绕过的环节就是IO.在Linux中,一切皆文件.实际上要文件干啥?不就是读写么? ...
- PHP 页面刷新与跳转的方法汇总
HTML meta标签 <meta http-equiv='content-type' content="text/html;charset=utf-8"/> 实现页面 ...
- vue自定义拖动指令
1.在项目开发中,需要对div进行拖动.因为需要自定义组件 a>定义全局拖拽指令: 定义全局指令,需要在main.js中写入vue.directive('drag',{});即可.但是一般会在外 ...
- Codeforces 805A/B/C
A. Fake NP 传送门:http://codeforces.com/contest/805/problem/A 本题是一个数学问题. 给定两个正整数l,r(l≤r),对于区间[l..r]上的任一 ...
- 淘宝的开源分布式文件系统TFS
TFS(Taobao FileSystem)是淘宝团队开源的海量非结构化数据存储设计的分布式系统.构筑在普通的Linux机器集群上,可为外部提供高可靠和高并发的存储访问.高可扩展.高可用.高性能.面向 ...
- 编写App测试用例的关注点
如何做到测试用例的百分百覆盖一直是测试用例编写过程中的难点,首先在测试时我们经常会遇见一些常见的bug,那么我们可以在编写测试用例时考虑到这些点. 一:关于业务逻辑 ...
- 使用PSI-probe监控tomcat7
http://www.lambdaprobe.org/ http://blog.csdn.net/tanglei6636/article/details/70169153 http://blog.cs ...
- Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...
- [Cypress] Stub Network Requests in a Cypress Test
To keep our tests fast and easily repeatable, it makes sense to create many integration tests and fe ...