Link to home
Start Free TrialLog in
Avatar of ian_willo
ian_willo

asked on

Searching & Setting Directories in Java

Hello i am producing a java program and in my program it involves chosing a directory to search for different file types.  Currently, i have created the code to search for a file, now i just need to edit this code in order to set a directory in which to search.

public class directory extends JFrame implements ActionListener
{
FileDialog f = new FileDialog(this, "Choose A Folder To Search For Files", FileDialog.LOAD); // Loads the file select box
}

public void actionPerformed(ActionEvent e)
{
 if(e.getSource() == rootDir){
f.setDirectory("M:\\Year 2");       // If root directory is selected go here
f.setVisible(true);
 }

So all i basiclly need to do is to select a dirctoty and subdirectories not files.

Thanks Ian.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

set the FileDialog.DIRECTORIES_ONLY flag
Avatar of jimmack
jimmack

>> select a dirctoty

You could get the selected directory (in the FileDialog) into a File object using:

File selectedDir = f.getDirectory();

listFiles() will give you a list of files and directories from a given directory.

You can then use isDirectory() on each entry to see if it is a directory or not.
Avatar of ian_willo

ASKER

What do you mean.  how do i implement that in my code??????????
Who are you asking?
You should stick to Swing if you're generally using Swing:

JFileChooser fileChooser = new JFileChooser(file);
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
either of you i dont understand what you mean. Only been doing java for about 1 and a half months
CEHJ where do i put this code mate
When you have selected the directory from either the JFileChooser or FileDialog, you want a File object.  From a JFileChooser, you can call getSelectedFile(), from FileDialog, you can call getDirectory(), but this only returns a String:

JFileChooser (let's call it jfc):

File selectedDir = jfc.getSelectedFile();

FileDialog (from your example - f)

File selectedDir = new File(f.getDirectory());

Now you have the File you can get the files/directories that it contains.  You can either create a filter for this method (to only return directories), or you can search the returned array for directories:

File[] dirContent = selectedDir.listFiles();

for (int i = 0; i < dirContent.length; i++)
{
    if (dirContent[i].isDirectory())
    {
    }
    else  // must be a file
    {
    }
}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me explain a bit more.  I have two buutons called movies and pictures.  Also a root dircetory button.  I want to click the root dir button to set the directory.  Then click the movies button to display mpgs and the pictures to display bmps.
Just call my code from actionPerformed. The API docs show how to set filters:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html
Does the root dir button open the file choosing dialog?

Where are the other buttons?

You might want to consider closing your old question: https://www.experts-exchange.com/questions/20776870/Open-GL-Beginner.html
If you want to set a root, create the chooser with a String representing where the root is (see API doc)
And here is an example of a FileFilter that the listFiles method can use to retrieve specified files from a directory:

private class MyFilter implements FileFilter
{
    private String suffix;

    public boolean accept(File pathname)
    {
        return (pathname.getName().endsWith(suffix));
    }

    public MyFilter(String suffix)
    {
        this.suffix = suffix;
    }
}

You could then get the .mpg files in a directory using:

File[] mgpFiles = currentDirectory.listFiles(new MyFilter("mpg"));
8-)