JFileChooser实例

运行后的效果如图:

 

源代码如下: 

package swing;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class FileChooserDemo1 implements ActionListener{

 JFrame f = null;
 JLabel label = null;
 JTextArea textarea = null;
 JFileChooser fileChooser = null;
    
 public FileChooserDemo1() {
  // TODO Auto-generated constructor stub
  f = new JFrame("FileChooser demo");
  Container contentPane = f.getContentPane();
  textarea = new JTextArea();
  JScrollPane scrollPane = new JScrollPane(textarea);
  scrollPane.setPreferredSize(new Dimension(350,300));
  
  JPanel panel = new JPanel();
  JButton b1 = new JButton("新建文件");
  b1.addActionListener(this);
  JButton b2 = new JButton("存储文件");
  b2.addActionListener(this);
  
  panel.add(b1);
  panel.add(b2);
  
  label = new JLabel("",JLabel.CENTER);
  
  fileChooser = new JFileChooser("d:\\");
  
  contentPane.add(label,BorderLayout.NORTH);
  contentPane.add(scrollPane,BorderLayout.CENTER);
  contentPane.add(panel,BorderLayout.SOUTH);
  
  f.pack();
  f.setVisible(true);
  
  f.addWindowListener(new WindowAdapter(){
   public void windowclosing(WindowEvent e){
    System.exit(0);
   }
   
  });
  
  
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  new FileChooserDemo1();
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  
  File file = null;
  int result;
  
  if(e.getActionCommand().equals("新建文件")){
   
   fileChooser.setApproveButtonText("确定");
   fileChooser.setDialogTitle("打开文件");
   result = fileChooser.showOpenDialog(f);
   
   textarea.setText("");
   
   if(result==JFileChooser.APPROVE_OPTION)
   {
    file = fileChooser.getSelectedFile();
    label.setText("您选择打开的文件名称为:"+file.getName());
   }else
    if (result == JFileChooser.CANCEL_OPTION)
    {
     label.setText("您没有选择任何文件");
    }
   FileInputStream fileInStream = null;
   
   if(file!= null)
   {
    try {
     fileInStream = new FileInputStream(file);
    } catch (FileNotFoundException e1) {
     // TODO Auto-generated catch block
     label.setText("file not found");
     return;
    }
    
    int readbyte;
    try {
     while((readbyte = fileInStream.read())!=-1)
     {
      textarea.append(String.valueOf((char)readbyte));
     }
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     label.setText("file read error");
     
    }
    finally
    {
     if(fileInStream!= null)
      try {
       fileInStream.close();
      } catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      }
    }
   }
  }
  
  if(e.getActionCommand().equals("存储文件"))
  {
   result = fileChooser.showSaveDialog(f);
   file = null;
   String fileName;
   
   if(result == JFileChooser.APPROVE_OPTION)
   {
    file = fileChooser.getSelectedFile();
    label.setText("您选择存储的文件名称为:"+file.getName());
   }
   else if(result == JFileChooser.CANCEL_OPTION)
   {
    label.setText("you do not choose any file");
   }
   
   FileOutputStream fileOutStream = null;
   if (file != null)
   {
    try {
     fileOutStream = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
     // TODO Auto-generated catch block
     label.setText("file not found");
     return;
    }
    
    String content = textarea.getText();
    
    try {
     fileOutStream.write(content.getBytes());
    } catch (IOException e1) {
     // TODO Auto-generated catch block
     label.setText("write file error");
    }
    finally
    {
     if(fileOutStream!= null)
      try {
       fileOutStream.close();
      } catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      }
    }
       
   }
  }
 }

}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值