一、题目描述-读取Jar文件属性
1、题目
做一个读取jar文件的内容和功能的工具。
2、解题思路
创建一个类:ReaderJarFrame
使用ReaderJarFrame继承JFrame构建窗体
在选择jar后,窗体的表格会显示其内容。
创建一个类:ReadJar
ReadJar类实现读取功能
读取的逻辑:使用JarFile类与Enumeration接口。
jarFile是Java提供的一个类,可以读取jar包中的所有文件(递归读取),然后提取指定的文件。
JarFile.entries()可以得到一个jar文件内部元素的迭代器(Enumeration)。同过这个迭代器我们可以获得所有的JarEntry
3、代码详解
ReaderJarFrame类
package com.xiaoxuzhu;
import java.awt.EventQueue;
import java.awt.FileDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class ReaderJarFrame extends JFrame {
private static final long serialVersionUID = 7776474232093298420L;
private JPanel contentPane;
private JTextField pathTextField;
private JTable table;
private LocalTableModel model = new LocalTableModel();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
ReaderJarFrame frame = new ReaderJarFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ReaderJarFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setTitle("获取Jar文件的文件属性");
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 262);
contentPane.add(panel);
panel.setLayout(null);
JLabel messageLabel = new JLabel("文件:");
messageLabel.setBounds(39, 25, 41, 15);
panel.add(messageLabel);
pathTextField = new JTextField();
pathTextField.setBounds(90, 22, 185, 21);
panel.add(pathTextField);
pathTextField.setColumns(10);
JButton button = new JButton("选择");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
do_button_actionPerformed(arg0);
}
});
button.setBounds(305, 21, 79, 23);
panel.add(button);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(30, 77, 372, 152);
panel.add(scrollPane);
table = new JTable(model);
scrollPane.setViewportView(table);
}
// 打开按钮的单击事件
@SuppressWarnings("rawtypes")
protected void do_button_actionPerformed(ActionEvent arg0) {
java.awt.FileDialog fd = new FileDialog(this);
fd.setVisible(true);
String path = fd.getDirectory() + fd.getFile();
if (path.endsWith(".jar")) {
pathTextField.setText(path);
List list = ReadJar.process(path);
for (int i = 0; i < list.size(); i++) {
FileName fileName = (FileName) list.get(i);
model.addRow(new Object[] { fileName.getName(), fileName.getSize() });
}
}
}
}
FileName类
package com.xiaoxuzhu;
public class FileName {
private String name;
private String size;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
LocalTableModel类:
package com.xiaoxuzhu;
import javax.swing.table.DefaultTableModel;
public class LocalTableModel extends DefaultTableModel {
private static final long serialVersionUID = 8746141681804141757L;
Class<?>[] types = new Class[] { java.lang.Object.class, java.lang.String.class };
boolean[] canEdit = new boolean[] { false, false };
public LocalTableModel() {
super(new Object[][] {}, new String[] { "文件", "大小" });
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
}
ReadJar类:
package com.xiaoxuzhu;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ReadJar {
@SuppressWarnings("rawtypes")
static List<FileName> process(String fileName) {
List<FileName> list = new ArrayList<FileName>(); // 创建List集合对象
try {
JarFile jarFile = new JarFile(fileName); // 创建JarFile对象
Enumeration en = jarFile.entries();
while (en.hasMoreElements()) { // 测试枚举中是否包含更多的元素
FileName file = new FileName(); // 定义JavaBean对象
JarEntry entry = (JarEntry) en.nextElement(); // 获取集合中的元素
String name = entry.getName(); // 获取文件名称
long size = entry.getSize(); // 获取文件大小
file.setName(name);
file.setSize(size + "");
list.add(file); // 将对象添加到集合中
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
读取结果:
以上就是Java实现读取Jar文件属性的方法详解的详细内容,更多关于Java读取Jar文件属性的资料请关注编程网其它相关文章!