检查目录中是否存在文件夹,并列出该特定文件夹Java中的所有文件

时间:2013-02-01 11:54:53

标签: java file jfilechooser

我正在使用JfileChooser进行文件夹选择。假设目录包含这些文件夹:

main
src
test

我希望程序自动选择“src”文件夹并列出其所有文件内容。如果文件夹不存在,我收到错误信息。这就是我到目前为止所做的事情。但是没有运气可以让它工作。任何帮助赞赏。

import java.awt.BorderLayout;


public class Iterate_Choose extends JFrame {

private JPanel contentPane;
private JTextField textField;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Iterate_Choose frame = new Iterate_Choose();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Iterate_Choose() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 645, 455);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    textField = new JTextField();
    textField.setColumns(10);
    final JTextArea textArea = new JTextArea();
    final JButton btnNewButton = new JButton("Choose File Directory");
    btnNewButton.addActionListener(new ActionListener() {
        @SuppressWarnings("null")
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);


            try {
               // Create a File object containing the canonical path of the desired directory


               File f = new File(new File(".").getCanonicalPath());
               String[] directories = f.list(new FilenameFilter() {
                   public boolean accept(File dir, String name) {
                     return dir.isDirectory();
                   }
                 });


                 System.out.println(Arrays.toString(directories));
                 for(int i=0;i<directories.length;i++){
                     if(directories[i].equals("src")){
                         File f1 = new File(new File(".").getCanonicalPath() + "\\" +directories[i]);
                         chooser.setCurrentDirectory(f1);

                     }
                     else{
                         System.out.println("Error Occured!!");
                     }
                 }


                 }



               // Set the current directory

             catch (IOException e1) {
               e1.printStackTrace();
            }

            // Show the dialog; wait until dialog is closed
            int returnVal = chooser.showOpenDialog(btnNewButton);

            if(returnVal == JFileChooser.APPROVE_OPTION)
            {
                File f = chooser.getSelectedFile();
                textField.setText(f.getAbsolutePath());

                File[] contents = f.listFiles();

                for(int file=0;file<contents.length;file++)
                {

                    if (contents[file].getName().endsWith(".java")) {
                        String s = contents[file].getName();



                    System.out.println(contents[file].getName());
                    //here you get the contents of the selected directory
                }
            }
            }
        }
    }

    );



    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addGap(40)
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                            .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField, GroupLayout.PREFERRED_SIZE, 325, GroupLayout.PREFERRED_SIZE)))
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 401, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(208, Short.MAX_VALUE))
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(114)
                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(31)
                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                .addGap(18)
                .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                .addContainerGap())
    );
    contentPane.setLayout(gl_contentPane);
}

}

得到的结果如下,这不是预期的结果:

[.classpath, .project, .settings, bin, src]
Error Occured!!
Error Occured!!
Error Occured!!
Error Occured!!

0 个答案:

没有答案
相关问题