文件选择器JFileChooser的创建

package src10;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.JTextArea;

public class s01 implements ActionListener {
	JFrame jf = null;
	JLabel jl = null;
	JTextArea jta = null;
	JFileChooser jfc = null;
	private Object Finally;
	public s01()  {
		jf = new JFrame("FileChooser Example");
		Container jcp = jf.getContentPane();
		jta = new JTextArea();
		JScrollPane jsp = new JScrollPane(jta);
		jsp.setPreferredSize(new Dimension(350, 300));
		JPanel jp = new JPanel();
		JButton jbt1 = new JButton("新建文件");
		JButton jbt2 = new JButton("存储文件");
		jbt1.addActionListener(this);
		jbt2.addActionListener(this);
		jp.add(jbt1);
		jp.add(jbt2);
		jl = new JLabel("", JLabel.CENTER);
		jfc = new JFileChooser("D:\\");
		jcp.add(jl, BorderLayout.NORTH);
		jcp.add(jsp, BorderLayout.CENTER);
		jcp.add(jp, BorderLayout.SOUTH);
		jf.pack();
		jf.setVisible(true);
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	public static void main(String[] args) {
		new s01();
	}
	public void actionPerformed(ActionEvent e) {
		File file = null;
		int result;
		FileInputStream fin = null;
		FileOutputStream fout = null;
		if(e.getActionCommand().equals("新建文件")) {
			jfc.setApproveButtonText("确定");
			jfc.setDialogTitle("打开文件");
			result = jfc.showOpenDialog(jf);
			jta.setText("");
			if(result==jfc.APPROVE_OPTION) {
				file = jfc.getSelectedFile();
				jl.setText("您选择打开的文件名称为: " + file.getName());
			} else if(result==jfc.CANCEL_OPTION) {
				jl.setText("您没有选择任何文件");
			}
		}
		if(file!=null) {
			try {
				fin = new FileInputStream(file);
			} catch (FileNotFoundException e1) {
				jl.setText("File not found");
				return;
			}
		}
		int readbyte;
		try {
			while((readbyte=fin.read())!=-1) {
				jta.append(String.valueOf((char)readbyte));
			}
		} catch (IOException e2) {
			jl.setText("读取文件错误");
		}
		finally {
			try {
				if(fin!=null) { 
					fin.close();
				}
			} catch (IOException e3) {}
		}
		if(e.getActionCommand().equals("存储文件")) {
			result = jfc.showSaveDialog(jf);
			file = null;
			String fileName;
			if(result==jfc.APPROVE_OPTION) {
				file = jfc.getSelectedFile();
				jl.setText("您选择存储的文件名称为: " + file.getName());
			} else if(result==jfc.CANCEL_OPTION) {
				jl.setText("您没有选择任何文件");
			}
		}
		if(file!=null) {
			try {
				fout = new FileOutputStream(file);
			} catch (FileNotFoundException e4) {
				jl.setText("File not found");
				return;
			}
			String content = jta.getText();
			try {
				fout.write(content.getBytes());
			} catch (IOException e5) {
				jl.setText("写入文件错误");
			}
			finally {
				if(fout!=null) {
					try {
						fout.close();
					} catch (IOException e6) {}
				}
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值