2 qq 22346115 qq_22346115 于 2015.06.07 15:39 提问

java编程,读取jar文件的属性,编到一半就进行不下去了,求大神帮忙

package cksj;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
public class JAR extends JFrame{
JFileChooser j=new JFileChooser();//文件选择器
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JLabel l=new JLabel("文件:",JLabel.CENTER);
JTextField t=new JTextField();
JButton b=new JButton("选择");
JFileChooser jfc=new JFileChooser();//文件选择器
public JAR(){
setTitle("截取Jar文件的文件属性");
Container c=getContentPane();
c.setLayout(new BorderLayout());//边界布局管理
p1.setLayout(new FlowLayout(FlowLayout.CENTER));// p1的对齐方式为居中对齐

c.add(p1,BorderLayout.NORTH); // 将数据面板p1以边界布局方式添加到内容窗格的上方
p2.setLayout(new GridLayout());
c.add(p2,BorderLayout.CENTER);
p1.add(l);
p1.add(t);
p1.add(b);

}
String[][] allW(int numb) throws Exception{
    String W[][];
    return W;
}
    public static void main(String[] args) {
        new JAR();
    }![图片说明](http://img.ask.csdn.net/upload/201506/07/1433662529_610836.png)

1个回答

tongyi55555
tongyi55555   2015.06.07 21:59

增加按钮的监听事件,并实现jar文件内的文件名和大小。代码如下:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class JarTest extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 2851420353664542764L;
    JFileChooser j = new JFileChooser();// 文件选择器
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JLabel l = new JLabel("文件:", JLabel.CENTER);
    JTextField t = new JTextField(20);
    JButton b = new JButton("选择");
    String[] columns = new String[] { "文件名", "文件大小" };
    JFileChooser jfc = new JFileChooser();// 文件选择器
    DefaultTableModel dtm = new DefaultTableModel(null, columns);
    JTable table = new JTable(dtm);

    public JarTest() {
        setTitle("截取Jar文件的文件属性");
        Container c = getContentPane();
        c.setLayout(new BorderLayout());// 边界布局管理
        p1.setLayout(new FlowLayout(FlowLayout.CENTER));// p1的对齐方式为居中对齐

        c.add(p1, BorderLayout.NORTH); // 将数据面板p1以边界布局方式添加到内容窗格的上方
        p1.add(l);
        p1.add(t);
        p1.add(b);
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                int flag = jfc.showOpenDialog(null);
                if (flag == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = jfc.getSelectedFile();
                    t.setText(selectedFile.getAbsolutePath());
                    String[][] tableContent = getJarContent(selectedFile);
                    dtm.setDataVector(tableContent, columns);
                }
            }
        });
        p2.setLayout(new GridLayout());
        c.add(p2, BorderLayout.CENTER);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(table);
        p2.add(scrollPane);
    }

    /**
     * 获取给定jar文件内的文件夹和文件名字、大小
     * @param selectedFile
     * @return
     */
    private String[][] getJarContent(File selectedFile) {
        String content[][] = null;
        HashMap<String, Long> nameSizeMap = new HashMap<String, Long>();
        try {
            JarFile jarFile = new JarFile(selectedFile);
            JarEntry jarEntry = null;
            Enumeration<JarEntry> enume = jarFile.entries();
            while (enume.hasMoreElements()) {
                jarEntry = (JarEntry) enume.nextElement();
                nameSizeMap.put(jarEntry.getName(), jarEntry.getSize());
            }
            List<Map.Entry<String, Long>> nameList = new ArrayList<Map.Entry<String, Long>>(
                    nameSizeMap.entrySet());
            Collections.sort(nameList,
                    new Comparator<Map.Entry<String, Long>>() {
                        public int compare(Map.Entry<String, Long> o1,
                                Map.Entry<String, Long> o2) {
                            return (o1.getKey()).toString().compareTo(
                                    o2.getKey());
                        }
                    });
            content = new String[nameList.size()][2];
            Entry<String, Long> entry = null;
            for (int i = 0; i < content.length; i++) {
                entry = nameList.get(i);
                content[i][0]= entry.getKey();
                content[i][1]= entry.getValue().toString();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return content;
    }

    public static void main(String[] args) {
        JarTest jt = new JarTest();
        jt.setSize(400, 400);
        jt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jt.setLocationRelativeTo(null);
        jt.setVisible(true);
    }
}

测试结果:
图片说明

Csdn user default icon
上传中...
上传图片
插入图片