java實現目錄選擇方法 JFileChooser
阿新 • • 發佈:2019-01-08
方法一,使用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();
}
- 多選
- 在基本用法中,設定
- c.setMultiSelectionEnabled(true);
- 即可實現檔案的多選。
- 讀取選擇的檔案時需使用
- File[] files = c.getSelectedFiles();
- 選擇目錄
- 利用這個開啟對話方塊,不僅可以選擇檔案,還可以選擇目錄。
- 其實,對話方塊有一個FileSelectionMode屬性,其預設值為“JFileChooser.FILES_ONLY”,只需要將其修改為“JFileChooser.DIRECTORIES_ONLY”即可。
- JFileChooser c = new JFileChooser();
- c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- c.setDialogTitle("Select path to save");
- int result = c.showOpenDialog(PrintDatetime.this);
- if (result == JFileChooser.APPROVE_OPTION) {
- String path = c.getSelectedFile().getAbsolutePath());
- }
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 ();
- 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;
- publicclass 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() {
- publicvoid windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- publicstaticvoid main(String[] args) {
- new s01();
- }
- publicvoid 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());
- } elseif(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());
- } elseif(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) {}
- }
- }
- }
- }
- }