JFileChooser and directories

I want to be able to open all the files in a directory and if there are any directories inside that, to open all the files in it, and so on and so on. The way I was thinking while opening the files, check if the current File is a file or directory and if it is a directory to recursively open the other files. Is this the best way to do it? If so, how do I check if the current File is a file or directory. If not, what do you suggest?

Here's one I prepared earlier...
   * returns the canonical name of the first ocurrence of seekFilename in or
   * under the given directory, using a recursive depth first file algorithm.
  public static String findFirst(File directory, String seekFilename) throws IOException {
    //tracer.debug("> "+directory);
    File[] files = directory.listFiles();
    if(files==null) return(null);
    for ( int i=0; i<files.length; i++ ) {
      if ( files.isFile() && files[i].getCanonicalPath().endsWith(seekFilename) ) {
//tracer.debug("< "+directory+" -> "+files[i].getCanonicalPath());
return(files[i].getCanonicalPath());
for ( int i=0; i<files.length; i++ ) {
if ( files[i].isDirectory() ) {
String classFilename = findFirst(files[i], seekFilename);
if(classFilename!=null) {
//tracer.debug("< "+directory+" -> "+classFilename);
return(classFilename);
//tracer.debug("< "+directory+" -> null");
return(null);

Similar Messages

  • JFileChooser - show files and directories only select directory

    I need a JFileChooser that will display directories and zip files but only allow a directory to be selected.
    basically i need to be able to select a certain directory that contains a certain zip file, so to make it easier to find i want the zip files to show up so i can find the correct directory...
    I tried...
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.setFileFilter(new FileFilter() {
                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    else if (f.getName().endsWith(".zip")) {
                        return true;
                    return false;
                public String getDescription() {
                    return "testing";
            });but that only displays directories in the GUI.
    The javadoc for the setFileSelectionMode() says, "Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY. " ....which makes you think that setting the file selection mode only alters what can be SELECTED...however it alters what is displayed as well.
    ...any ideas?
    thanks

    This should display the files of interest provided the filter is tuned to do so, and allow you to select either the file of interest or the folder... If you select the file, then folder it is contained is returned.
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    MyFileFilter filter = new MyFileFilter();
    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = chooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {              
      System.out.println(chooser.getCurrentDirectory());
    }

  • File Chooser is displaying names of the Files and Directories as boxes

    Hi All,
    Actually i am working on an application in which i have the requirement to internationalize the File Chooser in All Operating Systems. The application is working properly for all languages on MAC OS, but not working properly for the language Telugu on both the Ubuntu and Windows OS. The main problem is, when i try to open the File Chooser after setting the language to TELUGU all the labels and buttons in the File Chooser are properly internationalized but names of Files and directories in the File Chooser are displaying as boxes.
    So please provide your suggestions to me.
    Thanks in Advance
    Uday.

    I hope this can help you:
    package it.test
    import java.awt.BorderLayout;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Locale;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    * @author Alessandro
    public class Test extends JDialog {
         private JFileChooser fc = null;
         private Frame bfcParent = null;
         public Test(Frame parent, boolean modal) {
              super(parent, modal);
              this.bfcParent = parent;
              if (fc == null) {
                   fc = new JFileChooser();
                   fc.setAcceptAllFileFilterUsed(false);
                   fc.setLocale(Locale.ITALIAN);//i think you should use english
                   //these are in telugu
                   UIManager.put("FileChooser.openDialogTitleText", "Open Dialog");
                   UIManager.put("FileChooser.saveDialogTitleText", "Save Dialog");
                   UIManager.put("FileChooser.lookInLabelText", "LookIn");
                   UIManager.put("FileChooser.saveInLabelText", "SaveIn");
                   UIManager.put("FileChooser.upFolderToolTipText", "UpFolder");
                   UIManager.put("FileChooser.homeFolderToolTipText", "HomeFolder");
                   UIManager.put("FileChooser.newFolderToolTipText", "New FOlder");
                   UIManager.put("FileChooser.listViewButtonToolTipText", "View");
                   UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details");
                   UIManager.put("FileChooser.fileNameHeaderText", "Name");
                   UIManager.put("FileChooser.fileSizeHeaderText", "Size");
                   UIManager.put("FileChooser.fileTypeHeaderText", "Type");
                   UIManager.put("FileChooser.fileDateHeaderText", "Date");
                   UIManager.put("FileChooser.fileAttrHeaderText", "Attr");
                   UIManager.put("FileChooser.fileNameLabelText", "Label");
                   UIManager.put("FileChooser.filesOfTypeLabelText", "filesOfType");
                   UIManager.put("FileChooser.openButtonText", "Open");
                   UIManager.put("FileChooser.openButtonToolTipText", "Open");
                   UIManager.put("FileChooser.saveButtonText", "Save");
                   UIManager.put("FileChooser.saveButtonToolTipText", "Save");
                   UIManager.put("FileChooser.directoryOpenButtonText", "Open Directory");
                   UIManager.put("FileChooser.directoryOpenButtonToolTipText", "Open Directory");
                   UIManager.put("FileChooser.cancelButtonText", "Cancel");
                   UIManager.put("FileChooser.cancelButtonToolTipText", "Cancel");
                   UIManager.put("FileChooser.newFolderErrorText", "newFolder");
                   UIManager.put("FileChooser.acceptAllFileFilterText", "Accept");
                   fc.updateUI();
         public int openFileChooser() {
              fc.setDialogTitle("Open File");
              fc.resetChoosableFileFilters();
              int returnVal = 0;
              fc.setDialogType(JFileChooser.OPEN_DIALOG);
              returnVal = fc.showDialog(this.bfcParent, "Apri File");
              //Process the results.
              if (returnVal == JFileChooser.APPROVE_OPTION) {
                   System.out.println("Hai scelto di aprire un file");
              } else {
                   System.out.println("hai annullato l'apertura");
              return returnVal;
         private static void createAndShowGUI() {
              JFrame frame = new JFrame("FileChooser");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JPanel jp = new JPanel(new BorderLayout());
              JButton openButton = new JButton("Open File");
              final Test test = new Test(frame, true);
              openButton.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                        test.openFileChooser();
              openButton.setEnabled(true);
              jp.add(openButton, BorderLayout.AFTER_LAST_LINE);
              //Add content to the window.
              frame.add(jp);
              //Display the window.
              frame.pack();
              frame.setVisible(true);
         public static void main(String[] args) {
              //Schedule a job for the event dispatch thread:
              //creating and showing this application's GUI.
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        //Turn off metal's use of bold fonts
                        createAndShowGUI();
    }bye ale

  • JFileChooser and ensureFileIsVisible

    Hi
    I'm trying to get the ensureFileIsVisible method working with a JFileChooser but there are a few problems.
    I put in this line where the EnsureFileIsVisible is a class which implements PropertyChangeListener.
    fileChooser.setAccessory(new EnsureFileIsVisible(fileChooser));So every time a key is pressed the selected file is shown, and thats what I want.
    The problem is that the fileChooser should only allow files to be approved, so it shouldn't return when selecting a directory, it should open it.
    If I use: fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);...it will show both files and directories when pressing a key, but it also allows for directories to be selected, and not opened.
    When not using the above line, getSelectedFile() simply returns null if a directory
    is selected.
    Any solution to this?
    Thanks

    bump....

  • I have an external hard drive that was formatted by a PC and has files and directories etc. I want to format it and use it on my IMAC for backup but I can't seem to write to it nor can I delete current content. How do I initialize it for use with the MAC?

    I have an external hard drive that was formatted by a PC and has files and directories copied to it etc. I want to use it on my IMAC for backup. I see it on my my IMAC . I can open files etc.  But I can't seem to write to it nor can I delete current content. I don't care if I lose current content. How do I initialize it for use with the MAC?

    You can't write to it because it's formatted as NTFS which OS X will read but not write to. If you want to continue using the drive with both a PC and OS X you will need to download and install NTFS-3G so you can then write to it from your Mac. You can get NTFS-3G at:
    http://www.macupdate.com/app/mac/24481/ntfs-3g
    If you want to use the drive exclusively with your Mac then move the data off it and reformat it in Disk Utility (Applications - Utilities - Disk Utilities) as Mac OS Extended (Journaled.)

  • How can I remove all photos and directories from iPhoto?

    How can I remove all photos and directories from iPhoto as if it was a brand new install?
    I am new to iPhoto and was unaware of how it works, honestly I still am a little confused as to when pics I am using 1/2 the time, some from iPhoto and some from MyPics.
    Anyhow, I am looking to remove all photos from iPhoto because during the migration of all my photos from PC to MAC, somehow my timeline became messed up and all my photos that have been logged my dates since 1998 are now all messed up.
    Although all my year folders are still in tact, IE 1998-2006, the subfolders and like pics are all intertwined somehow.
    And when I search iPhoto for a given year, photos from 2,4, 8 yrs back show up in 2006.
    Anyhow, I have selected and deleted all photos and folders within iPhoto, and then import the desired photos from MyPics, only to have the photos double up in the library. ( The old ones that should be deleted and the new imports)
    I simply don't get it, they fill the trash and disappear when it's emptied, then reappear when iPhoto re-opens!
    Can someone help????
    I have completely reworked all my photos so that they are all in the correct folders outside of iPhoto, and now I want to import those and only those pics with that file structure.
    IE Structure = YEAR > MONTH > DAY ( or if needed) > DAY/EVENT

    Do you want to remove the existing full sized image files also? If so then just drag the iPhoto Library folder from the Pictures folder to the Trash and empty it. But be warned you'll lose all of your image files.
    You are dragging the files to the iPhoto Trash bin and then using the iPhoto->Empty Trash menu, right?
    In copying your photos to myspace and then back the original capture date may not be retained and then either the created date or modified date will be used by iPhoto. If the files in the current library are the originals from your camera then those have the best chance of retaining the correct Captured Date. You can check that out by dragging the Originals folder from your iPhoto Library folder to the desktop and then trash what remains of the iPhoto Library folder. Then launch iPhoto and it will ask you to create an new library. Then import that Originals folder and it's subfolders by dragging it into iPhoto's open window. You'll keep the same rolls that was. The roll dates may change but you can check the captured date of the files by using the calendar in the Source pane.
    You won't be able to keep your folder structure within iPhoto. Each outside folder will result in a roll with the same title as the folder. You'll be able to create folders in the Source pane to put albums inside. You can create a smart album to include all photos in a certain date range, line one month.
    Do you Twango?
    TIP: For insurance against the iPhoto database corruption that many users have experienced I recommend making a backup copy of the Library6.iPhoto database file and keep it current. If problems crop up where iPhoto suddenly can't see any photos or thinks there are no photos in the library, replacing the working Library6.iPhoto file with the backup will often get the library back. By keeping it current I mean backup after each import and/or any serious editing or work on books, slideshows, calendars, cards, etc. That insures that if a problem pops up and you do need to replace the database file, you'll retain all those efforts. It doesn't take long to make the backup and it's good insurance.
    G5 Dual Core 2GHz, 2G RAM, 250G HD; G4 Dual 1Ghz, 1.5G RAM, 80G HD,   Mac OS X (10.4.7)   22 LCD Display, 200G & 160G FW HDs, Canon S400, i850 & LIDE 50, Epson R200

  • How to select  file and directories to ZIP

    HI,
    I'm doing an application that would zip a file and directories. here's an exmple:
    fileName = FileNameTest.csv
    folderName = FileNameTest_A
    folderName = FileNameTest_B
    The foldername is taken from the fileName with "_A" and "_B" at the end. The file and directories are located in a directory where there can also be other files and directories in it. What I want to do is to zip only the above file and directories.
    Any help will be greatly appreciated!
    Thanks!

    Not a very clear specification. What do you want the resulting zip entries/files/paths to look like?

  • JFileChooser and FileFilters

    I have the following problem:;
    I have a JFileChooser and I attached a FileFilter to it, which only allows files with the extension *.in and *.inx. Everything works well - only files with these extensions are shown in the Dialog.
    But as a user I expect, that it is not a fault if I only type in the name of the file, because I expect the programme to add the extension automatically. For example in an OpenDialog, the user sees the file test.inx and the types in "test", because he/she thinks that the extension will be added.
    This is not the case...and which is even worse, the call of fileChooser.getSelectedFiles() will return an empty array. Hence there is no possibility for the programme to add the file extension.....
    How can solve this problem?? What is my mistake?? I cannot image that Swing is not able to do this, because this common FileDialog behaviour....
    I thank everybody who tries to help me...

    The quick and dirty workaround would be to create a "Save" dialog (setDialogType method), set the Button texts to whatever you want, the same for title. When the dialog returns, you can check the file (getSelectedFile()), and if it does not exist, try the same name with the extension(s).

  • Traversing files and directories underneath a directory

    I'm looking at some sample code about traversing files and directories underneath a directory. I'm trying to understand exactly what the code is doing. I see that dir.list() is building a string array of filenames and directory names. Then it tests each to see whether it's a directory. If it's a directory then is looks and each file and or directory in that directory.
    But I don't understand how it keeps going below just the second level directory. The two lines of code I'm not sure I understand are:
    1) process(dir);
    2) visitAllDirsAndFiles(new File(dir, children));
    Can anyone help me to understand exactly how this code block can traverse beyond just the second directory level?
    // Process all files and directories under dir
        public static void visitAllDirsAndFiles(File dir) {
            process(dir);
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (int i=0; i<children.length; i++) {
                    visitAllDirsAndFiles(new File(dir, children));

    No, I guess they might be assuming somethings. Here is the page with the sample code:
    [http://exampledepot.com/egs/java.io/TraverseTree.html]

  • How to bundle all files and directories in a package?

    how to bundle all files and directories in a package?
    i plan to put all image files, .class files and some other files together.and then double click the package to execute the program.
    should i use jar?is there a link for tutorial or example?
    thanks in advance

    http://java.sun.com/docs/books/tutorial/jar/

  • What files and directories are shipped with the Oracle Agent ?

    Hi:
    - Could someone please outline the intent of all the files
    and directories that are shipped with the Oracle agent?
    - I think this was documented somewhere but I cannot
    recall where I saw this previously.
    - Another question: if I wanted to add a logfile for my plug-in
    (for debugging/troubleshooting)
    under which directory should this logfile be placed ?
    Thanks
    John

    http://download.oracle.com/docs/cd/B19306_01/em.102/b40002/structure.htm#i1028189 should help you understand the directory structure of OEM Agent.

  • Listing Files and Directories

    On our internal web page we have some links to various areas
    like Office Forms, Safety, etc... The supervisors that maintain
    these documents have access to shares that allow them add, remove,
    and update the files as needed. Since the content in each changes
    fairly often we're currently using CFDIRECTORY with the LIST action
    to display the contents to the users on the web page.
    Some of the areas now contain quite a few documents and the
    supervisors would like to be able to group their documents in
    subdirectories. I know I can I can use RECURSE="yes" to have the
    web page list the contents of these folders, but the supervisors
    would like the end users to see the subdirectories as well so
    things are less confusing and cluttered on both ends.
    So is there any way in CF to set up these pages so that
    subdirectories can can be navigated and the documents selected? If
    possible, I'd like something that can handle this dynamically so
    that I don't have to constantly change the code every time a
    supervisor decides to add/remove a directory.
    We're currently running CF8 Standard.
    Thanks!

    Vidikron wrote:
    > On our internal web page we have some links to various
    areas like Office Forms,
    > Safety, etc... The supervisors that maintain these
    documents have access to
    > shares that allow them add, remove, and update the files
    as needed. Since the
    > content in each changes fairly often we're currently
    using CFDIRECTORY with the
    > LIST action to display the contents to the users on the
    web page.
    >
    > Some of the areas now contain quite a few documents and
    the supervisors would
    > like to be able to group their documents in
    subdirectories. I know I can I can
    > use RECURSE="yes" to have the web page list the contents
    of these folders, but
    > the supervisors would like the end users to see the
    subdirectories as well so
    > things are less confusing and cluttered on both ends.
    >
    > So is there any way in CF to set up these pages so that
    subdirectories can can
    > be navigated and the documents selected? If possible,
    I'd like something that
    > can handle this dynamically so that I don't have to
    constantly change the code
    > every time a supervisor decides to add/remove a
    directory.
    >
    > We're currently running CF8 Standard.
    >
    > Thanks!
    >
    Pretty much what you got should do it. The simplest change
    would be to
    output entire directory structure on your current page, just
    nesting the
    files and directories.
    I would just create a looping output that uses the fields of
    the record
    set to output the directories and the files with-in them
    probably as a
    nested set of <ul> unordered lists.
    If you want to break up this user interface into distinct
    units which
    only list the contents of a current directory then just do
    the output as
    you have it without using recursion. But make the
    sub-directory
    listings links that when clicked repopulate the current page
    listing the
    contents of the selected directory.
    Neither should take a great deal of rework to your current
    interface.

  • After a kernel panic my log says hfs removed orphaned or unlinked files and directories

    Today, September 14, I had my first ever kernel panic on a MacBook Pro that was purchased in July of this year. After reboot, a file that I use every day seemed to be missing all updates since September 6. There were no newer versions in my TimeMachine backups either. During my investigation to determine how that could be, I found these entries in my log of the post-panic reboot:
    Sep 14 12:49:12 localhost kernel[0]: hfs: mounted xxx on device root_device
    Sep 14 12:49:12 localhost kernel[0]: hfs: mounted xxx on device root_device
    Sep 14 12:49:12 localhost kernel[0]: hfs: Removed 596 orphaned / unlinked files and 1920 directories
    Sep 14 12:49:14 xxx kernel[0]: hfs: mounted Recovery HD on device disk0s3
    Sep 14 12:49:14 xxx kernel[0]: hfs: unmount initiated on Recovery HD on device disk0s3
    Sep 14 12:49:15 xxx kernel[0]: hfs: early journal init: volume on disk1s2 is read-only and journal is dirty.  Can not mount volume.
    Sep 14 12:49:31 xxx kernel[0]: hfs: Removed 0 orphaned / unlinked files and 91 directories
    Sep 14 12:49:31 xxx kernel[0]: hfs: mounted MacBook Backup B on device disk1s2
    The removed orphans and directories seem very concerning to me. Is there a way to tell what was removed and why?
    What should I do next?

    You are getting a kernel panic because you are trying to boot the computer with a Tiger DVD. That model requires 10.6.3 - Snow Leopard. You can purchase replacement discs from Apple:
    Apple Store Customer Service at 1-800-676-2775 or visit online Help for more information.
    To contact product and tech support: Apple - Support - Contact Apple Support.
    For Mac App Store: Apple - Support - Mac App Store.
    For iTunes: Apple - Support - iTunes.

  • Connections to target databases and directories

    Hi All,
    I'm creating a few custom provisioning connectors to databases and directories using the GTC framework -- http://download.oracle.com/docs/cd/E10391_01/doc.910/e10360/custom_prov.htm#Toc153968045.
    I'm using the initialize() method in the Transport Provider to do a few things including establishing a connection to the target system. The sendData() is used to actually update the target.
    Unfortunately, every time a task in the process definition is called, a new instance of the Transport Provider is created, which means the initialize() method will be called. This becomes an issue when you have a huge number of users being updated in OIM because for each user at least one new connection will be created and destroyed and creating new connections is generally an expensive operation.
    So I'm wondering if there's any way to keep a persistent class within OIM that would hold the connection to the target system so it wouldn't have to be recreated with every change.
    Thanks

    If you do not perform the full list of tasks for every action, then you will need to bulk load the users. For OIM, every instance of a task runs independent of other tasks. So if you have 500 new users being onboarded, you will get 500 instances of the Create User. You cannot change this.
    If you want to customize the connector, you could possibly create a JDBC connection pool to be used instead but then you are on your own for the custom coding and everything.
    -Kevin

  • Communication between flash and Director

    hi,
    I want to communicate between flash exe and director exe
    using local connection object. Can anybody help me in this, by
    telling the code for it. I tried a lot using it by different ways
    but I am not succesful in it yet
    Thanx in advance

    hi,
    I want to communicate between flash exe and director exe
    using local
    connection object. Can anybody help me in this, by telling
    the code for it. I
    tried a lot using it by different ways but I am not succesful
    in it yet
    Thanx in advance
    ANSWER :
    you should make an action script to the animation & etc.
    you made in your FLASH by a name as like as this : (I made a pause
    Button & named it gPause);
    on (release) {
    getURL("event:gPause");
    and then in youe director file make a MovieScript including
    the following code :
    on startMovie
    member("name of member you imported from Flash as swf. file
    ").model("Bip01").bonesplayer.pause()
    end
    on gPause
    member("name of member you imported from Flash as swf. file
    ").model("Bip01").bonesplayer.pause()
    end

Maybe you are looking for

  • Recreating itunes library when itunes 10 won't open?

    So when clicking the iTunes 10 icon in the dock the icon bounces once then quits before the application will open. I've re-downloaded itunes 10, reinstalled OSX (I'm having an iPhoto problem too which seemed to justify this), removed a 3rd party visu

  • Sharing music between different accounts on a single computer

    I have read the instructions here: http://support.apple.com/kb/HT1203 , but I'm still having a problem. Scenario: Two Ipods, one PC (WinXP), one big music library, originally created and stored directly in C:\My Music, which is accessible to all user

  • Save as older version of PDF

    One of our users wants me to save a pdf file in 1.6 (Acrobat 7) format.  They also want the form to print duplex, flip on long edge.  It seems that whatever way I save the form in an older version (i.e. configure distiller, reduce file size or optimi

  • Customizing Date Formats in PSE 7 on Vista 64

    I've just installed PSE 7 on Vista 64 SP1 (previously I was running it on XP SP1. It seems to work very well but I now find I'm unable to customize the date format the way I like it ("Sun 11 Jan 2009"). Previously I'd used the trick of creating a DWo

  • Newby trying to capturing SCD Type 2 data

    Hi all, OWB Client 10.2.0.1.31 OWB Repos 10.2.0.1.0 Target DW DB Oracle 10.2.0.3.0 I've been working with OWB for 2+ years now but what we currently do is very simple mappings and process flows only. Now I need to start capturing history data. I've d