相信很多人跟我一样,苦于在各种包之间,不知道Class存在什么地方,为此,自己写了一个小工具,来寻找目录下的Class文件

支持 目录查询,支持带包路径查询

入口Entrance.java

package com.freud.looking;

import java.io.IOException;

/**
* Entrance which contains Main Method
*
* @author Freud
*
*/
public class Entrance { public static void main(String[] args) throws IOException { UIFrame entrance = new UIFrame();
entrance.initThePanel();
entrance.repaint();
}
}

Logic.java

package com.freud.looking;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; /**
* 1. Get all the ended tails file 2. Validate the jar files which contains the
* condition classes 3. Validate the zip files which contains the condition
* classes
*
* @author Freud
*
*/
public class Logic { /**
* Traversal all the file to get end with tail's file
*
* @param file
* Files
* @param tail
* End tail
* @return Matched files
*/
public List<File> traversalFiles(File file, String tail) { List<File> files = new ArrayList<File>(); if (file.isDirectory()) {
for (File descendant : file.listFiles()) { for (File define : traversalFiles(descendant, tail)) { if (define.getName().endsWith(tail)) {
files.add(define);
} }
}
} else { if (file.getName().endsWith(tail)) {
files.add(file);
} } return files; } /**
* Validate the jar files for the condition classes
*
* @param files
* Jar file
* @param condition
* Needed classes
* @return Validated files
* @throws IOException
*/
public Set<String> validateJarFile(List<File> files, String condition) throws IOException { Set<String> flag = new HashSet<String>(); for (File file : files) { try {
JarFile jarFile = null;
try {
jarFile = new JarFile(file); Enumeration<JarEntry> jarentrys = jarFile.entries(); while (jarentrys.hasMoreElements()) { JarEntry jarEntry = (JarEntry) jarentrys.nextElement(); String name = jarEntry.getName().replace("/", ".").replace("\\", "."); if (name.contains(condition)) {
flag.add(jarFile.getName());
} if (name.contains(condition.replace("/", "."))) {
flag.add(jarFile.getName());
} if (name.contains(condition.replace("\\", "."))) {
flag.add(jarFile.getName());
} } } finally {
if (jarFile != null)
jarFile.close();
}
} catch (Exception e) {
System.out.println("Error Occured in File - " + file.getAbsolutePath());
}
}
return flag;
} /**
* Validate the zip files for the condition classes
*
* @param files
* Zip file
* @param condition
* Needed classes
* @return Validated files
* @throws IOException
*/
public Set<String> validateZipFile(List<File> files, String condition) throws IOException { Set<String> flag = new HashSet<String>(); for (File file : files) {
try {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file); @SuppressWarnings("unchecked")
Enumeration<ZipEntry> zipentrys = (Enumeration<ZipEntry>) zipFile.entries(); while (zipentrys.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) zipentrys.nextElement(); String name = zipEntry.getName().replace("/", ".").replace("\\", "."); if (name.contains(condition)) {
flag.add(zipFile.getName());
} if (name.contains(condition.replace("/", "."))) {
flag.add(zipFile.getName());
} if (name.contains(condition.replace("\\", "."))) {
flag.add(zipFile.getName());
}
}
} finally {
if (zipFile != null) {
zipFile.close();
}
} } catch (Exception e) {
System.out.println("Error Occured in File - " + file.getAbsolutePath());
}
}
return flag;
}
}

UIFrame.java

package com.freud.looking;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List; import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField; /**
* Show the main component and do Submit logic
*
* @author Freud
*
*/
public class UIFrame extends JFrame { /**
* serialVersionUID
*/
private static final long serialVersionUID = 1L; /**
* Components For the main frame
*/
private JLabel pathLable;
private JTextField pathField;
private JLabel conditionLable;
private JTextField conditionField;
private JLabel tailLabel;
private JCheckBox jarCheckBox;
private JCheckBox zipCheckBox;
private JLabel resultLabel;
private JTextField resultField;
private JButton submit; /**
* Constructor
*/
public UIFrame() { /**
* Main Frame initialization
*/
this.setTitle("Looking for Classes!");
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null); } /**
* Initialization the main panel's components
*
* @return status
*/
public boolean initThePanel() { try { /**
* Location defined
*/
pathLable = new JLabel("Path Of the Location!");
pathLable.setBounds(10, 10, 350, 20); pathField = new JTextField();
pathField.setBounds(10, 40, 350, 30); tailLabel = new JLabel("Tails");
tailLabel.setBounds(10, 80, 350, 20); jarCheckBox = new JCheckBox(".jar", true);
jarCheckBox.setBounds(10, 110, 50, 30); zipCheckBox = new JCheckBox(".zip");
zipCheckBox.setBounds(60, 110, 80, 30); conditionLable = new JLabel("Condition");
conditionLable.setBounds(170, 80, 100, 20); conditionField = new JTextField();
conditionField.setBounds(170, 110, 150, 30); resultLabel = new JLabel("Result Show --");
resultLabel.setBounds(10, 150, 350, 20); resultField = new JTextField();
resultField.setBounds(10, 180, 350, 30); submit = new JButton("Submit");
submit.setBounds(120, 220, 100, 30);
submit.addActionListener(new ActionListener() { /**
* Submit operations
*/
@Override
public void actionPerformed(ActionEvent e) { try {
String tail = "";
String condition = conditionField.getText(); Logic logic = new Logic(); StringBuffer sb = new StringBuffer(); /**
* Check the jar file box
*/
if (jarCheckBox.isSelected()) { tail = ".jar"; /**
* Get all files ended with ".jar"
*/
List<File> files = logic.traversalFiles(new File(pathField.getText()), tail); int i = 0; /**
* Validate the files which have the condition
* classes
*/
for (String item : logic.validateJarFile(files, condition)) {
if (i == 0) {
sb.append(item);
} else {
sb.append(";").append(item);
} i++; }
} /**
* Check the zip file box
*/
if (zipCheckBox.isSelected()) { tail = ".zip"; /**
* Get all files ended with ".zip"
*/
List<File> files = logic.traversalFiles(new File(pathField.getText()), tail); /**
* Validate the files which have the condition
* classes
*/
for (String item : logic.validateZipFile(files, condition)) {
if (sb.toString().equals("")) {
sb.append(item);
} else {
sb.append(";").append(item);
}
}
} /**
* If no files contains
*/
if (sb.toString().equals("")) {
sb.append("No matched file find!");
} /**
* Set result field
*/
resultField.setText(sb.toString()); } catch (Exception e1) {
/**
* Error handling
*/
resultField.setText("Error Occured : " + e1.getMessage());
} }
}); this.add(pathLable);
this.add(pathField);
this.add(conditionLable);
this.add(conditionField);
this.add(tailLabel);
this.add(jarCheckBox);
this.add(zipCheckBox);
this.add(resultLabel);
this.add(resultField);
this.add(submit); this.repaint(); return true; } catch (Exception e) {
return false;
}
}
}

一个寻找.jar 和.zip文件中class文件的工具的更多相关文章

  1. jar命令+7z:创建,替换,修改,删除Jar, war, ear包中的文件

    虽然现在已经有各种智能的IDE可以为我们生成jar包,war包,ear包,甚至带上了自动替换,部署的功能.但一定会有那么些时候,你需要修改或是替换jar包,war包,ear包中的某个文件而不是整个重新 ...

  2. 文件_ _android从资源文件中读取文件流并显示的方法

    ======== 1   android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...

  3. pip freeze > requirements.txt` 命令输出文件中出现文件路径而非版本号

    pip freeze > requirements.txt 命令输出文件中出现文件路径而非版本号 解决办法: pip list --format=freeze > requirements ...

  4. 推荐一个SAM文件或者bam文件中flag含义解释工具

    SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...

  5. 推荐一个SAM文件中flag含义解释工具--转载

    SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...

  6. VS2010在C#头文件中添加文件注释的方法

    步骤: 1.VS2010 中找到安装盘符(本人安装目录在D盘,所以以D盘为例)D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\I ...

  7. VS2010在C#头文件中添加文件注释的方法(转)

    步骤: 1.VS2010 中找到(安装盘符以D盘为例)D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTempl ...

  8. 31、SAM文件中flag含义解释工具--转载

    转载:http://www.cnblogs.com/nkwy2012/p/6362996.html  SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这 ...

  9. python移动多个子文件中的文件到一个文件夹

    import os import os.path import shutil def listDir(dirTemp): if None == dirTemp: return global nameL ...

随机推荐

  1. AOP和IOC理解

    在百度上看到一篇很有意思的文章,是对AOP的一种解释,如下:(摘自:百度文库的 AOP和IOC最容易理解的说明(Spring知识小计)): IOC,依赖倒置的意思, 所谓依赖,从程序的角度看,就是比如 ...

  2. __attribute__ 详解

    GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__attribute__可以设置函数属性(Function    Attribute).变量属性(Variable Att ...

  3. Java按正则提取字符串

    在Java开发中,有时会遇到一些比较别扭的规则从字符串中提取子字符串,规则无疑是写正则表达式来表达了,那按照正则来提取子字符串就会用到java.util.regex包. java.util.regex ...

  4. Google java编程技术规范

    不遵循规范的程序猿,不是好的coder. 学习java有一段时间了,一直想找java编程技术规范来学习一下,幸而网络资源丰富,各路玩家乐于分享,省去了好多麻烦,姑且算站在网友的肩上,砥砺前行. /** ...

  5. Codeforces Round #198 (Div. 2) —— D

    昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...

  6. 李洪强iOS开发之-cocopods安装

  7. 嵌入式C语言不可不用的关键字

    1.static关键字 这个关键字前面也有提到,它的作用是强大的. 要对static关键字深入了解,首先需要掌握标准C程序的组成. 标准C程序一直由下列部分组成: 1)正文段——CPU执行的机器指令部 ...

  8. 透过表象看本质!?之二——除了最小p乘,还有PCA

    如图1所示,最小p乘法求得是,而真实值到拟合曲线的距离为.那么,对应的是什么样的数据分析呢? 图1 最小p乘法的使用的误差是.真实值到拟合曲线的距离为 假如存在拟合曲线,设直线方程为.真实值到该曲线的 ...

  9. 9.png(9位图)在android中作为background使用导致居中属性不起作用的解决方法

    在使用到9.png的布局上面添加 android:padding="0dip" 比如 <LinearLayout            android:layout_widt ...

  10. 产品设计中先熟练使用铅笔 不要依赖Axure

    在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...