package com.hss.chap13;import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;public class Chap13_03 { /**
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
ZipFrame zf = new ZipFrame();
zf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
zf.setVisible(true);
}
}); }}class ZipFrame extends JFrame{
public ZipFrame(){
this.setBounds(200, 200, 500, 400);
this.setLayout(new BorderLayout());
//工具栏布局
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu("file");
JMenuItem jmi1 = new JMenuItem("open");
JMenuItem jmi2 = new JMenuItem("exit");
jm.add(jmi1);
jm.add(jmi2);
jmb.add(jm);
this.setJMenuBar(jmb);
//添加处理事件
jmi1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("."));
int r = jfc.showOpenDialog(ZipFrame.this);
if(r==JFileChooser.APPROVE_OPTION){
zipname=jfc.getSelectedFile().getPath();
try {
scannerFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
jmi2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//添加textarea
jta = new JTextArea();
jsc = new JScrollPane(jta);
//添加combobox
fileCombo = new JComboBox();
fileCombo.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
try {
loadFile((String)fileCombo.getSelectedItem());
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
this.add(jsc,BorderLayout.CENTER);
this.add(fileCombo,BorderLayout.SOUTH);
}
public void scannerFile() throws IOException{
//先将combox清空
fileCombo.removeAllItems();
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipname));
ZipEntry ze ;
while((ze=zis.getNextEntry())!=null){
fileCombo.addItem(ze.getName());
zis.closeEntry();
}
zis.close();
}
public void loadFile(String filename) throws Exception{
//先将显示区域清空
jta.setText("");
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipname));
ZipEntry ze;
while((ze=zis.getNextEntry())!=null){
System.out.println(ze.getName()+"---"+filename);
if(ze.getName().equals(filename)){
Scanner in = new Scanner(zis);
while(in.hasNextLine()){
String s = in.nextLine();
jta.append(s);
jta.append("\n");
}
}
zis.closeEntry();
}
zis.close();
}
private String zipname;
private JTextArea jta;
private JScrollPane jsc;
private JComboBox fileCombo;
}
代码可以执行, loadFile方法报java.lang.IllegalArgumentException,查了一下有的说是线程问题,有的地方说是参数问题,请大家帮我分析一下,异常原因。
解决方案 »
- 帮我算个java的时间问题,保证给分
- java如何调用ping语句 求解答
- jce 鉴别问题
- java 泛型问题?
- Java 2 图形设计卷Ⅱ:SWING》在网上找的怎么都是不全?
- 相当疑惑
- JAVA编写CA认证中的.KEYSTORE的问题
- 誰能教我一下在 Websphere studio application developer5.0裡自帶的appSever 裡配置一個oracle連接池,並用一個jsp頁面訪問連接池,100
- 涉及到“生产者-消费者”的 数据库存取 问题设计!(有结果比给分)
- 有谁知道Intellij Idea 2.5.2 注册 名称 密码
- 求救大虾们,写的复制文件的程序问题到底出在哪里???
- 求助String转JSONObject问题
public static File zipFile(String zipName, File[] files) {
File file = new File(zipName); try {
ZipOutputStream out = new ZipOutputStream(file); for(int i = 0; i < files.length; i++) {
if(files[i] == null) {
continue;
} FileInputStream in = new FileInputStream(files[i]);
ZipEntry entry = new ZipEntry(files[i].getName().replaceAll("\\[.+\\]", ""));
out.putNextEntry(entry);
int len = 0;
byte[] bytes = new byte[256]; while((len = in.read(bytes, 0, bytes.length)) != -1) {
out.write(bytes, 0, len);
} out.closeEntry();
in.close();
} out.flush();
out.close();
}
catch(Exception e) {
e.printStackTrace();
} return file;
}