java实现目录选择方法 JFileChooser

java实现目录选择两种方法:JFileChooser和DirectoryDialog

  (2011-11-01 15:09:47)
标签: 

杂谈

分类: Java

java实现目录选择的几种方法

    (2009-10-28 23:13:32)
标签:  

java

   

目录选择

   

it

分类: 编程技术

方法一,使用JFileChooser控件:

示例:选文件

JFileChooser chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置只能选择目录
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
  selectPath =chooser.getSelectedFile().getPath() ;

  System.out.println ( "你选择的目录是:" + selectPath );
  chooser.hide();
}


  1. 多选
  2. 在基本用法中,设置
  3. c.setMultiSelectionEnabled(true);
  4. 即可实现文件的多选。
  5. 读取选择的文件时需使用
  6. File[] files = c.getSelectedFiles();
  7. 选择目录
  8. 利用这个打开对话框,不仅可以选择文件,还可以选择目录。
  9. 其实,对话框有一个FileSelectionMode属性,其默认值为“JFileChooser.FILES_ONLY”,只需要将其修改为“JFileChooser.DIRECTORIES_ONLY”即可。
  10. JFileChooser c = new JFileChooser();
  11. c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  12. c.setDialogTitle("Select path to save");
  13. int result = c.showOpenDialog(PrintDatetime.this);
  14. if (result == JFileChooser.APPROVE_OPTION) {
  15. String path = c.getSelectedFile().getAbsolutePath());
  16. }

j2me 中使用

方法二,使用DirectoryDialog控件:

示例:

Display display = new Display ();
Shell shell = new Shell (display);

 DirectoryDialog dialog = new DirectoryDialog (shell);
 selectPath = dialog.open() ;
 System.out.println ( "你选择的目录是:" + selectPath );
 while (!shell.isDisposed()) {
        if (!display.readAndDispatch ()) display.sleep ();      

 }
 display.dispose ();



  1. package src10;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Container;  
  5. import java.awt.Dimension;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.event.WindowAdapter;  
  9. import java.awt.event.WindowEvent;  
  10. import java.io.File;  
  11. import java.io.FileInputStream;  
  12. import java.io.FileNotFoundException;  
  13. import java.io.FileOutputStream;  
  14. import java.io.IOException;  
  15.   
  16. import javax.swing.JButton;  
  17. import javax.swing.JFileChooser;  
  18. import javax.swing.JFrame;  
  19. import javax.swing.JLabel;  
  20. import javax.swing.JPanel;  
  21. import javax.swing.JScrollPane;  
  22. import javax.swing.JTextArea;  
  23.   
  24. public class s01 implements ActionListener {  
  25.     JFrame jf = null;  
  26.     JLabel jl = null;  
  27.     JTextArea jta = null;  
  28.     JFileChooser jfc = null;  
  29.     private Object Finally;  
  30.     public s01()  {  
  31.         jf = new JFrame("FileChooser Example");  
  32.         Container jcp = jf.getContentPane();  
  33.         jta = new JTextArea();  
  34.         JScrollPane jsp = new JScrollPane(jta);  
  35.         jsp.setPreferredSize(new Dimension(350300));  
  36.         JPanel jp = new JPanel();  
  37.         JButton jbt1 = new JButton("新建文件");  
  38.         JButton jbt2 = new JButton("存储文件");  
  39.         jbt1.addActionListener(this);  
  40.         jbt2.addActionListener(this);  
  41.         jp.add(jbt1);  
  42.         jp.add(jbt2);  
  43.         jl = new JLabel("", JLabel.CENTER);  
  44.         jfc = new JFileChooser("D:\\");  
  45.         jcp.add(jl, BorderLayout.NORTH);  
  46.         jcp.add(jsp, BorderLayout.CENTER);  
  47.         jcp.add(jp, BorderLayout.SOUTH);  
  48.         jf.pack();  
  49.         jf.setVisible(true);  
  50.         jf.addWindowListener(new WindowAdapter() {  
  51.             public void windowClosing(WindowEvent e) {  
  52.                 System.exit(0);  
  53.             }  
  54.         });  
  55.     }  
  56.     public static void main(String[] args) {  
  57.         new s01();  
  58.     }  
  59.     public void actionPerformed(ActionEvent e) {  
  60.         File file = null;  
  61.         int result;  
  62.         FileInputStream fin = null;  
  63.         FileOutputStream fout = null;  
  64.         if(e.getActionCommand().equals("新建文件")) {  
  65.             jfc.setApproveButtonText("确定");  
  66.             jfc.setDialogTitle("打开文件");  
  67.             result = jfc.showOpenDialog(jf);  
  68.             jta.setText("");  
  69.             if(result==jfc.APPROVE_OPTION) {  
  70.                 file = jfc.getSelectedFile();  
  71.                 jl.setText("您选择打开的文件名称为: " + file.getName());  
  72.             } else if(result==jfc.CANCEL_OPTION) {  
  73.                 jl.setText("您没有选择任何文件");  
  74.             }  
  75.         }  
  76.         if(file!=null) {  
  77.             try {  
  78.                 fin = new FileInputStream(file);  
  79.             } catch (FileNotFoundException e1) {  
  80.                 jl.setText("File not found");  
  81.                 return;  
  82.             }  
  83.         }  
  84.         int readbyte;  
  85.         try {  
  86.             while((readbyte=fin.read())!=-1) {  
  87.                 jta.append(String.valueOf((char)readbyte));  
  88.             }  
  89.         } catch (IOException e2) {  
  90.             jl.setText("读取文件错误");  
  91.         }  
  92.         finally {  
  93.             try {  
  94.                 if(fin!=null) {   
  95.                     fin.close();  
  96.                 }  
  97.             } catch (IOException e3) {}  
  98.         }  
  99.         if(e.getActionCommand().equals("存储文件")) {  
  100.             result = jfc.showSaveDialog(jf);  
  101.             file = null;  
  102.             String fileName;  
  103.             if(result==jfc.APPROVE_OPTION) {  
  104.                 file = jfc.getSelectedFile();  
  105.                 jl.setText("您选择存储的文件名称为: " + file.getName());  
  106.             } else if(result==jfc.CANCEL_OPTION) {  
  107.                 jl.setText("您没有选择任何文件");  
  108.             }  
  109.         }  
  110.         if(file!=null) {  
  111.             try {  
  112.                 fout = new FileOutputStream(file);  
  113.             } catch (FileNotFoundException e4) {  
  114.                 jl.setText("File not found");  
  115.                 return;  
  116.             }  
  117.             String content = jta.getText();  
  118.             try {  
  119.                 fout.write(content.getBytes());  
  120.             } catch (IOException e5) {  
  121.                 jl.setText("写入文件错误");  
  122.             }  
  123.             finally {  
  124.                 if(fout!=null) {  
  125.                     try {  
  126.                         fout.close();  
  127.                     } catch (IOException e6) {}  
  128.                 }  
  129.             }  
  130.         }  
  131.     }  
  132. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值