背景:

重新做项目的过程中,引用了大量旧代码。尤其是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. marquee图片无缝拼接滚动

    marquee图片无缝滚动 先了解一下对象的几个的属性: innerHTML:     设置或获取位于对象起始和结束标签内的 HTML scrollHeight: 获取对象的滚动高度. scrollL ...

  2. mysql 怎样先排序再分组

    权游游牧族:众所周知!一句SqL语句不能先排序再分组.所以这里给出几个案例 --表结构-- create table `shop` ( `id` int (10) PRIMARY KEY, `shop ...

  3. Java中indexof()的使用

    Java中字符串中子串的查找共有四种方法(indexof()) indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置.如果没有找到子字符串,则返回-1. 如果 starti ...

  4. mybatis源码阅读-SqlSessionFactory和SqlSession(三)

    说明 读了3遍:https://my.oschina.net/zudajun/blog/665956 现在统一整理成笔记 并跟着源码一行一行调试 统一整理起来 SqlSession 接口定义 publ ...

  5. [bzoj2466][中山市选2009]树_树形dp

    树  bzoj-2466 中山市选-2009 题目大意:给定一棵树,每一个点有一个按钮和一个灯泡.如果按下一个点的按钮那么和这个点直接相连的点包括这个点的灯泡的状态会改变.如果是点亮就会变成熄灭,如果 ...

  6. SQL Server 运行计划操作符具体解释(2)——串联(Concatenation )

    本文接上文:SQL Server 运行计划操作符具体解释(1)--断言(Assert) 前言: 依据计划.本文開始讲述另外一个操作符串联(Concatenation).读者能够依据这个词(中英文均可) ...

  7. EularProject 36:2进制和10进制回文数

    华电北风吹 天津大学认知计算与应用重点实验室 完毕日期:2015/7/29 Double-base palindromes Problem 36 The decimal number, 585 = 1 ...

  8. luogu3369 【模板】 普通平衡树 Splay

    题目大意 维护一个数据结构,满足以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多个相同的数,因输出最小的排名) 查询排名为x ...

  9. Reset and Clear Recent Items and Frequent Places in Windows 10

    https://www.tenforums.com/tutorials/3476-reset-clear-recent-items-frequent-places-windows-10-a.html ...

  10. (转)Android开发书籍推荐:从入门到精通系列学习路线书籍介绍

    Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 转自:http://blog.csdn.net/findsafety/article/details/52317506 很多时候我们都会不 ...