背景:

重新做项目的过程中,引用了大量旧代码。尤其是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. Google Shell Style Guide

    转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...

  2. 在oracle中将某个字段的数据作为列名的查询

    原表结构 查询语句: select sno,sname,sum(语文) 语文,sum(数学) 数学,sum(英语) 英语 from (select sno,sname,decode(subjiect, ...

  3. Emacs学习笔记之主模式笔记

    % 模式相关—————————————————————————— 这里经常用的也就各种语言相对应的模式,有些就没再写进去,比如f90等模式 Fundamental mode 基本模式         ...

  4. 输入输出流String间的转换

    来自:http://wuhongyu.iteye.com/blog/806791 1.String  to  inputStream InputStream is = new ByteArrayInp ...

  5. php unlink()函数使用

    最近在写个网站,需要上传图片,如果修改图片,就图片就没有用了,会占用服务器的硬盘资源,所以想到用unlink函数删除旧照片. 问题 : unlink函数只能删除 相对于函数执行文件的相对目录  或  ...

  6. Spring Boot-整合Mybatis(五)

    原始的整合方式 1.添加依赖 <!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot< ...

  7. rsync在windows下的安装和配置

    rsync分为服务器端和客户端,以A(服务器端),B(客户端)2台服务器为例 A的IP地址为192.168.1.111 B的ip地址为192.168.1.1231, 先配置服务器端,在服务器上安装cw ...

  8. Ruby 读取文件

    Ruby 读取文件 一次全读出来 textAll = File.read("fileName.txt") puts textAll 一次读取一行 file = File.open( ...

  9. 2015年趋势科技笔试A卷

    题目原题来源:url=BHz9dr7Dbql5Ai0fTaUsi8QH-ieA9UAtw8kpf-Us_cGUnsz7ZIU1SfHIp33Cphcp0n6uPikWL6r8n0a0zQ0wNOMLG ...

  10. css让文字旋转270度

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...