Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Find List of Files Located in a Folder in Java?

List Files in Folder in Java

Condition for Finding List of Files Located in a Folder in Java

  • Description: To find a list of files located in a folder in Java, the File class from the java.io package can be used. A File object is created representing the folder, and the listFiles() method is called on this object to retrieve an array of File objects representing the files and directories within the folder. To filter out only the files (excluding directories), a check can be performed using File.isFile() on each element of the array. Alternatively, Files.walk() from the java.nio.file package can be used for more advanced directory traversal, providing a stream of files that can be filtered or processed as needed. Both approaches allow for retrieving files in the specified folder, with the option to filter by file extensions or perform other criteria-based searches.
Sample Source Code
  • # ListOfFiles.java
    package JavaSamples;

    import java.io.File;

    public class ListOfFiles extends javax.swing.JFrame {

    public ListOfFiles() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("List of Files In Folder");

    jLabel2.setText("Folder Path :");

    jButton1.setText("Choose Folder");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jButton2.setText("List Of Files");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(125, 125, 125)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel2)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(121, 121, 121)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(62, 62, 62)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(142, 142, 142)
    .addComponent(jButton2)))
    .addContainerGap(56, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(23, 23, 23)
    .addComponent(jLabel1)
    .addGap(40, 40, 40)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel2)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addComponent(jButton1)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
    .addComponent(jButton2)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(36, 36, 36))
    );

    pack();
    }
    //
    //GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    javax.swing.JFileChooser folderChooser = new javax.swing.JFileChooser();
    folderChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
    int returnValue = folderChooser.showOpenDialog(this);
    if (returnValue == javax.swing.JFileChooser.APPROVE_OPTION) {
    String selectedFolderPath = folderChooser.getSelectedFile().getAbsolutePath();
    jTextField1.setText(selectedFolderPath);
    }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String FolderPath = jTextField1.getText();

    File folder = new File(FolderPath);

    File[] files = folder.listFiles();

    if (files != null) {
    for (File file : files) {
    if (file.isFile()) {
    jTextArea1.append("File: " + file.getName() + "\n");
    }
    }
    } else {
    jTextArea1.append("Folder is empty or does not exist.\n");
    }

    }//GEN-LAST:event_jButton2ActionPerformed

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
    new ListOfFiles().setVisible(true);
    });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
Screenshots
  • STEP 1: The user selects a folder from local files.
  • The user selects a folder from local files.

  • STEP 2: To click the choose folder button, it navigates the local device to choose a folder.
  • To click the choose folder button, it navigates the local device to choose a folder.

  • STEP 3: The folder path will be displayed in the text field.
  • The folder path will be displayed in the text field.

  • STEP 4: Once the user clicks the 'List Of Files' button, it automatically lists out all the files in the selected folder.
  • Listing files in the selected folder.