-
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.
-
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
-
aweomse. Thanks.
I understand recursion, the listFiles method was the one I needed. Thanks a lot.
Similar Threads
-
By Arjuna in forum Database
Replies: 2
Last Post: 07-25-2007, 03:22 AM
-
Replies: 2
Last Post: 09-08-2005, 06:38 AM
-
Replies: 1
Last Post: 12-05-2001, 06:12 AM
-
By Peter in forum VB Classic
Replies: 0
Last Post: 06-15-2000, 07:53 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks