相信很多人跟我一样,苦于在各种包之间,不知道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. Cocos2d-x 3.0 beta 中加入附加项目,解决无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”

    Cocos2d-x 3.0 Alpha 1开始 对目录结构进行了整合.结果有些附加项目也被在项目中被精简出去. 比如说如果你需要使用CocoStdio导出的JSON.或使用Extensions扩展库, ...

  2. 创建基于文件组的数据库SQL救命语句

    CREATE DATABASE Sales ON PRIMARY (NAME = SPri1_dat, FILENAME = 'D:\SQLDB\SPri1dat.mdf', SIZE , MAXSI ...

  3. CAS单点登录配置[1]:准备工作

    关于CAS是什么这里就不在赘述,网友将它比喻成旅游景点的套票,买了一个套票就可以观看所有景点,不需要一个景点买一次票...我们重点介绍CAS单点登录的配置. 工具/原料 1.配置好JDK环境,否则不方 ...

  4. Jquery 获取表单值如input,select等方法

    1 if($("input[name=item][value='val']").attr('checked')==true) //判断是否已经打勾 name即控件name属性,va ...

  5. 三年所有JAVA技术文档列表

    学习工作流workflow管理基础概念.pdf 深入浅出Struts2(PDF).zip 深入浅出Hibernate.pdf 敏捷软件开发:原则.模式与实践.rar 精通Oracle.10g.Pl.S ...

  6. perl 导出函数到当前名字空间

    Vsftp:/root/perl/7# cat scan1.pm package scan1; require Exporter; @ISA = qw(Exporter); @EXPORT_OK = ...

  7. hadoop多机安装HA+YARN

    HA 相比于Hadoop1.0,Hadoop 2.0中的HDFS增加了两个重大特性,HA(热备)和Federation(联邦).HA即为High Availability,用于解决NameNode单点 ...

  8. bzoj3530

    比较恶心的题目不难发现是在自动机上做数位dp注意要考虑前导0,题目中给出的233是幸运数,20233不是为此我非常猥琐的写了一个四维dp,用记忆化搜索实现 ; ..,..,..,..] of long ...

  9. bzoj1486

    这里学了一个新知识叫分数规划这道题目是求∑w[i]/S最小首先二分答案k,然后如果某个环∑w[i]/S<=k即∑w[i]<=k*S-->∑w[i]-k*S<=0--->∑ ...

  10. ecshop 模板与库文件lbi

    转: 管理中心的"设置模板"其实就是把特定的[库项目]添加到模板中的[可编辑区域],或从[可编辑区域]中删除/修改某些[库项目],后台的"设置模板"只能对添加特 ...