File / Directory Management


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: File / Directory Management

  1. #1
    Join Date
    Mar 2005
    Posts
    24

    File / Directory Management

    I assume there's an easy way to do this.. but I'm not familiar enough w/ the File (and company) classes..

    I'm in a Windows environment, which I wouldn't imagine would make a difference.. but..

    I'm trying to open and read in all the files in a directory. My Question is:
    how do I load in the existing file names. I understand about BufferedReaders and everything, I just don't know how to get a list of all the files that exist in the current directory. I'm actually going to be doing branches of multiple layers of directories to extract all the files in all the directories.. so any help with that is much appreciated.

    Thanks a lot.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here is a directory scan with GUI.

    It uses recursion (a method that re-invokes itself). This again
    leads to an ArrayList of ArrayLists of ArrayLists (etc.) Where each ArrayList
    represents a directory, The ArrayLists inside these ArrayLists are
    tre subdirectories. The recursive methods are searchPath for collecting the
    data and showSearchResults( for listing the data.

    When it comes to directories scan there really isn't another (sensible ) way
    of doing this.

    Check it out.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    /**
     * Directory resursion example
     * @author sjalle
     * @version 1.0
     */
    
    public class RecursePath  extends JFrame implements ActionListener {
      BorderLayout borderLayout1 = new BorderLayout();
      JPanel contolPanel = new JPanel();
      JScrollPane reportScroll = new JScrollPane();
      JTextArea reportTA = new JTextArea();
      JTextField pathTF = new JTextField();
      JButton selectPathBtn = new JButton();
      JButton exitBtn = new JButton();
      JButton startBtn = new JButton();
      public RecursePath() {
        try {
          jbInit();
          selectPathBtn.addActionListener(this);
          exitBtn.addActionListener(this);
          startBtn.addActionListener(this);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        RecursePath rp = new RecursePath();
        rp.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        rp.pack();
        rp.setVisible(true);
      }
      /**
       * Recursive path search
       * @param dir
       */
      private ArrayList searchPath(String dir) {
        ArrayList list=new ArrayList();
        File f=new File(dir);
        File [] fList=f.listFiles();
        for (int i=0; i<fList.length; i++) {
    
          if (fList[i].isDirectory()) {
            list.add(searchPath(fList[i].getAbsolutePath()));
          } else {
            if (list.size()==0) {
              list.add("Dir:"+dir);
            }
            list.add(fList[i]);
          }
        }
        return list;
      }
      /**
       * Recursive arraylist extraction
       * @param aList
       */
      private void showSearchResults(ArrayList aList) {
        for (int i=0; i<aList.size(); i++) {
          Object ob=aList.get(i);
          if (ob instanceof ArrayList) { // a directory
            reportTA.append("\r\n");
            showSearchResults((ArrayList)ob);
          } else {  // a file
            showFileStats(ob.toString());
          }
        }
      }
      /**
       * print tabulated filename & size
       * @param filePath
       */
      private void showFileStats(String filePath) {
        File f=new File(filePath);
        reportTA.append(filePath+"\t"+f.length()+"\r\n");
      }
      /**
       * Handle buttons callback
       * @param e
       */
      public void actionPerformed(ActionEvent e) {
        if (e.getSource()==exitBtn) System.exit(0);
        if (e.getSource()==startBtn) {
          String s=pathTF.getText().trim();
          if (s.length()==0) return;
          ArrayList aList=searchPath(s);
          reportTA.setText("");
          showSearchResults(aList);
        } else if (e.getSource()==selectPathBtn) {
          FileDialog fd=new FileDialog(this);
          fd.setVisible(true);
          String dir=fd.getDirectory();
          if (dir==null) return;
          pathTF.setText(dir);
        }
      }
      /**
       * Initialize GUI JBuilder style
       * @throws Exception
       */
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        reportTA.setFont(new java.awt.Font("Dialog", 1, 13));
        reportTA.setText("no data yet");
        reportTA.setTabSize(8);
        pathTF.setText("");
        pathTF.setColumns(15);
        selectPathBtn.setText("Select Path");
        exitBtn.setText("Exit");
        startBtn.setText("Start");
        contolPanel.setPreferredSize(new Dimension(419, 35));
        reportScroll.setPreferredSize(new Dimension(410, 370));
        this.getContentPane().add(contolPanel, BorderLayout.SOUTH);
        contolPanel.add(pathTF, null);
        contolPanel.add(selectPathBtn, null);
        contolPanel.add(startBtn, null);
        contolPanel.add(exitBtn, null);
        this.getContentPane().add(reportScroll, BorderLayout.CENTER);
        reportScroll.getViewport().add(reportTA, null);
      }
    
    
    }
    Last edited by sjalle; 10-31-2005 at 01:04 AM.
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    24
    aweomse. Thanks.

    I understand recursion, the listFiles method was the one I needed. Thanks a lot.

Similar Threads

  1. Database not properly created
    By Arjuna in forum Database
    Replies: 2
    Last Post: 07-25-2007, 03:22 AM
  2. Replies: 2
    Last Post: 09-08-2005, 06:38 AM
  3. Replies: 1
    Last Post: 12-05-2001, 06:12 AM
  4. Comdlg32, select a FILE from FONTS directory!
    By Peter in forum VB Classic
    Replies: 0
    Last Post: 06-15-2000, 07:53 AM
  5. Replies: 3
    Last Post: 05-31-2000, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center