Showing all files of a given directory

I'm trying to write a class that shows me all files of a given directory. When a run the class without parameters, everything seems to work fine, however, when I use a parameter that contains the directory, I get the following error:
java.lang.NullPointerException
     at be.hogelimb.ti.fundamentals.io.IOProgr4.<init>(IOProgr4.java:25)
     at be.hogelimb.ti.fundamentals.io.IOProgr4.main(IOProgr4.java:43)
Exception in thread "main"
this is my class without parameters:
public class IOProgr4 {
public IOProgr4(){
File dirName = new File("C:\\Documents and Settings\\FirstName LastName\\My documents\\test");
     if (dirName.exists()){
     if (dirName.isDirectory()){
     File[] dirFiles = dirName.listFiles();
     for (int i = 0; i<dirFiles.length; i++){
     System.out.println(dirFiles);
     else{
     System.out.println("dirName is not a directory");
     else{
     System.out.println("Can'f find directory");
     public static void main(String args[]){
          new IOProgr4();
You see, it's a very simple class.
This is my class with parameters :
public class IOProgr4 {
String dirString;
public IOProgr4(String dirString){
this.dirString = dirString;
File dirName = new File(dirString);
if (dirName.exists()){
if (dirName.isDirectory()){
     File[] dirFiles = dirName.listFiles();
     for (int i = 0; i<dirFiles.length; i++){
     System.out.println(dirFiles[i]);          
     else{
     System.out.println("dirName is not a directory");
     else{
     System.out.println("Can't find directory");
public static void main(String args[]){
String dirString = "";
for (int i = 0; i < args.length; i++){
dirString += args[i] + " ";
new IOProgr4(dirString);
I'm working with Eclipse and my parameter string is the same as the String I've written in the first class, and it's also recognized as a directory. Can anyone tell me why this doesn't work?
Thanks in advance!!!

I'm working with Eclipse and my parameter string is
the same as the String I've written in the first
class...In the end, it has nothing to do with whether you've hard-coded the directory name versus taken it as a parameter. It must not have been the same value, or it would have worked the same. So your problem lies elsewhere - however, you were given a nice stack trace pointing out the line in your code where it happened. Are you not familiar with a little concept known as debugging?

Similar Messages

  • How to retrieve all files in a given directory?

    Hi all,
    I am a newbie to Java. I have to retrieve all files in a given directory. The names of the files are not known to me. Only the name of the directory is known. How can I retrieve and read all those files?
    Gary

    Check out the list and listFiles method: http://java.sun.com/j2se/1.3/docs/api/java/io/File.html

  • How to list all files in a given directory?

    How to list all the files in a given directory?

    A possible recursive algorithm for printing all the files in a directory and its subdirectories is:
    Print the name of the directory
    for each file in the directory:
    if the file is a directory:
    Print its contents recursively
    else
    Print the name of the file.
    Directory "games"
    blackbox
    Directory "CardGames"
    cribbage
    euchre
    tetris
    The Solution
    This program lists the contents of a directory specified by
    the user. The contents of subdirectories are also listed,
    up to any level of nesting. Indentation is used to show
    the level of nesting.
    The user is asked to type in a directory name.
    If the name entered by the user is not a directory, a
    message is printed and the program ends.
    import java.io.*;
    public class RecursiveDirectoryList {
    public static void main(String[] args) {
    String directoryName; // Directory name entered by the user.
    File directory; // File object referring to the directory.
    TextIO.put("Enter a directory name: ");
    directoryName = TextIO.getln().trim();
    directory = new File(directoryName);
    if (directory.isDirectory() == false) {
    // Program needs a directory name. Print an error message.
    if (directory.exists() == false)
    TextIO.putln("There is no such directory!");
    else
    TextIO.putln("That file is not a directory.");
    else {
    // List the contents of directory, with no indentation
    // at the top level.
    listContents( directory, "" );
    } // end main()
    static void listContents(File dir, String indent) {
    // A recursive subroutine that lists the contents of
    // the directory dir, including the contents of its
    // subdirectories to any level of nesting. It is assumed
    // that dir is in fact a directory. The indent parameter
    // is a string of blanks that is prepended to each item in
    // the listing. It grows in length with each increase in
    // the level of directory nesting.
    String[] files; // List of names of files in the directory.
    TextIO.putln(indent + "Directory \"" + dir.getName() + "\":");
    indent += " "; // Increase the indentation for listing the contents.
    files = dir.list();
    for (int i = 0; i < files.length; i++) {
    // If the file is a directory, list its contents
    // recursively. Otherwise, just print its name.
    File f = new File(dir, files);
    if (f.isDirectory())
    listContents(f, indent);
    else
    TextIO.putln(indent + files[i]);
    } // end listContents()
    } // end class RecursiveDirectoryList
    Cheers,
    Kosh!

  • Functional module to get the File from a given Directory

    Hi all,
    I am using a FM name 'subst_get_file_list' to get the file from a given directory but it is accepting only 40 Character length file only my requirement is to accept file name other than 40 char,
    give me good sugestion
    regards
    paul

    Hi Paul,
    Check the Function Module Gayathri has given. ie. 'SO_SPLIT_FILE_AND_PATH'.
    In the exporting parameter FULL_NAME , give the path name and in the importing parameter stripped_name , you will get the filename.
    Check this code.
    REPORT ZSHAIL_SPLITFILE.
    data: it_tab type filetable with header line,
          gd_subrc type i.
    tables: rlgrap.
    data: path type string,
          file_name type string.
    parameters file_nam type rlgrap-filename .
    data: user_act type i.
    at selection-screen on value-request for file_nam.
    CALL METHOD cl_gui_frontend_services=>file_open_dialog
      EXPORTING
        WINDOW_TITLE            = 'select a file'
       DEFAULT_EXTENSION       = '*.txt
        DEFAULT_FILENAME        = ''
        FILE_FILTER             = '*.txt'
        INITIAL_DIRECTORY       = ''
        MULTISELECTION          = abap_false
       WITH_ENCODING           =
      CHANGING
        file_table              = it_tab[]
        rc                      = gd_subrc
        USER_ACTION             = user_act
       FILE_ENCODING           =
      EXCEPTIONS
        FILE_OPEN_DIALOG_FAILED = 1
        CNTL_ERROR              = 2
        ERROR_NO_GUI            = 3
        NOT_SUPPORTED_BY_GUI    = 4
        others                  = 5
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    if user_act = '0'.
    loop at it_tab.
    file_nam = it_tab-filename.
    endloop.
    endif.
    path = file_nam.
    CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
      EXPORTING
        full_name           = path
    IMPORTING
       STRIPPED_NAME       = file_name
      FILE_PATH           =
    EXCEPTIONS
      X_ERROR             = 1
      OTHERS              = 2
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    at selection-screen.
    message i001(zmess) with file_name.
    Regards,
    SP.

  • Finder doesn't show all files

    When I add new photos to iPhoto, they do not appear as a file or item in Finder Documents list. I thought Finder was supposed to show all files and items that you create?

    santra, use EasyFind or Find File instead. BY default, Apple's own Finder/spotlight doesn't search everywhere and doesn't find everything (Mac fanboys will tell you this is a feature and is so intelligent that some people can't understand how smart it is; I prefer to think of it as a defect).
    Easyfind is here: http://www.versiontracker.com/dyn/moreinfo/macosx/8707
    Find File is here: http://www.versiontracker.com/dyn/moreinfo/macosx/10906315
    I hope this helps you to find all your stuff.

  • Listing of all file names in a directory

    Hello everyone,
    Is there a way to get the listing of all file names in a directory
    pointed by an entry in dba_directory in oracle into a collection in
    pl/sql procedure.
    Thank you.
    Tuncay

    this is the same problem I am trying to solve now. I found some ehlp in Tom Kyte articles on java stored procedures.
    create global temporary table DIR_LIST
    ( filename varchar2(255) )
    on commit delete rows;
    create and compile java source named "DirList" as
    import java.io.*;
    import java.sql.*;
    public class DirList
    public static void getList(String directory)
    throws SQLException
    File path = new File( directory );
    String[] list = path.list();
    String element;
    for(int i = 0; i < list.length; i++)
    element = list;
    #sql { INSERT INTO DIR_LIST (FILENAME)
    VALUES (:element) };
    create procedure get_dir_list( p_directory in varchar2 )
    as language java
    name 'DirList.getList( java.lang.String )';
    then you call in your pl/sql and fill your collection from the DIR_LIST table:
    get_dir_list('the name of the directory' );
    ---then you fill the collection by fetching the DIR_LIST table
    good luck,
    Florin

  • I previously had PS CS5 Trial installed but removed it using both Control Panel Programs and deleting all files from the CS5 directory on Program Files.  I then installed a purchased product from Adobe disc using the serial number on the case.  But when I

    I previously had PS CS5 Trial installed but removed it using both Control Panel Programs and deleting all files from the CS5 directory on Program Files.  I then installed a purchased product from Adobe disc using the serial number on the case.  But when I try to launch it, I get a screen headed Photoshop CS5 Extended Trial.  It asks for the serial number and says that my trial has expired.  It rejects the serial number from the Adobe case saying "This Serial Number is not valid for this product".  It thinks that I still have the expired Trial Version installed.  How do I convince it (the program) that the Trial Version is long gone and the currently installed product was installed with a valid and accepted serial number????

    Let me know how it goes. Go into your Adobe account and register your serial number asap.
    https://www.adobe.com/account/my-products-services.html
    That way, should you lose it or you need support, it is on record that it's yours and you can fetch your s/n it from anywhere.
    There are too may sad stories here, where people forgot and the box is buried or lost.
    Gene

  • Photos that have been deleted to aperture trash cannot be viewed. Any assistance is welcome in fixing this bug. The showing all files option is selected in the filter. System is 10.8.4, aperture version 3.4.5

    Photos that have been deleted to aperture trash cannot be viewed. Any assistance is welcome in fixing this bug. The showing all files option is selected in the filter. System is 10.8.4. Aperture version 3.4.5. Used to be able to view the files then all of a sudden they have stop appearing. Once the aperture trash is emptied the files can be viewed in the system trash. Becomes quite annoying if you want to review the aperture trash files before deletion.

    Photos that have been deleted to aperture trash cannot be viewed  ...
    The showing all files option is selected in the filter.
    Cookiemonster, previously Frank Caggiano's magic helped in this situation - have you tried the double, double Caggiano as described here?
        Images not appearing in browser, search filter is cleared
    https://discussions.apple.com/docs/DOC-3062
    In the most recent version I cannot get this trick to work, however. But try it anyway, maybe it is just my macthat has an issue.
    Did this happen directly after the update? Where is your library located, on an external or an internal drive? Are your originals managed or referenced?
    I'd try next to check, if this is caused by the library, by creating a small test library and trying to trash pictures there. If it works well in a new library try to repair the database and the permissions on your regular library.  See:
    http://documentation.apple.com/en/aperture/usermanual/index.html#chapter=27%26se ction=10%26tasks=true
    Regards
    Léonie

  • Finder dose not show all files

    finder dose not show all files and is not open.
    nothing is shown on the desktop.
    how is it fixed?

    Could be disk corruption or bad perference file (s).
    You may want to run these "standard" fixes if the problem persists.
    1) Check the amount of free space on your harddrive.  You should have a several gigs free.
    2) You should run disk utility
         Macintosh-HD -> Applications -> Utilities -> Disk Utility
         a) verify the disk
         b) update your permissions.
    3) Try a safe boot.
        Shutdown your machine.  Hold down the shift key.  Poweron.  Wait awhile Wait awhile while you harddrive
          is being checked.
        http://support.apple.com/kb/ht1455
    4) Define a new account & see if the problem occurs on that account.

  • How to select/ show all files NOT currently in a Collection?

    Is there an easy way to show all files in a catalog not currently assigned to ANY (non-smart) collection?
    Can i set up a Smart Collection to do this?

    thanks!  i should have mentioned that is the technique (excluding a,b,c,d,...) i currently use.
    i rephrased the question more specifically, looking for a more elegant/ LR "aware" way of doing this over here:
    http://forums.adobe.com/message/4111849#4111849

  • CS4 Bridge not showing all files

    I'm working on a 1TB drive in a directory with about 1000 dng files.  When use Bridge not all files are shown and there are no filters selected.  I go into the directory using Windows Explorer and they are indeed there.  I can select the file in Windows Explorer and open it in Photoshop without any problems.  Any help would be appreciated.

    Strange, when I set the option to show reject and hidden files nothing changed.  When I set the show items from subfolders all the images appeared.  The strange part is that they are all in the same directory.  At least it works now.  Thank you for your help, I appreciate it.

  • Finder does not show all files from NAS

    Hi there
    I have a NAS attached to my mini where music, ... is stored. I can access it from both the mac as well as a PC.
    for some reason the mac finder does not display all the files in the directory even though the files are there (i can see and open them when accessing the NAS through the PC). at the moment I only see this happen in a folder that contains video (mpg) files and some event must have triggered this strange behaviour because I could play the file sometime ago without problem.
    also, some of the files are shown as a folder than a file in the finder. E.g. a clip called last_holiday.mpg would show as a folder (when you open it it is empty) rather than a video file.
    I have tried to re-mount the share, but this has no effect.
    Is this a bug or is there some wrong setting?
    thanks for any help,
    Chris

    I have this same problem with mounted DAV server folders. I can see the files via Windows and other DAV clients -- just not via Finder. Moreover, it does not apply to all files: some of them do show up. And, it does not happen all of the time: just occasionally.

  • Deleting all files in the Unix directory

    I was trying to create a DB in Unix. I had to remove all the files in the udump directory and I saw that there were files in thousands. I was trying to find a command to remove all of them to make the directory empty, so that I could create the udump file for Database creation. However, I couldn't find a command to remove all of them at once. I tried to google but evey website speaks about just these commands..
    rm filename
    or
    rm -r * directory_name
    This doesn't solve my problem as these commands are deleting one file at a time and not all the files at once within the directory. Can someone tell me the solution?

    Hi,
    First go to the directory where you want to delete all the files and folders. then
    rm -fr directory_name
    rm -r is not neccessary that it will delete the files and folder.
    thx
    pradeep

  • How  to read all files  under a folder directory in FTP site

    Hi Experts,
    I use this SQL to read data from a file in FTP site. utl_file.fopen('ORALOAD', file_name,'r');
    But this need to fixed file name in a directory. However, client generate output file with auto finename.
    SO do we have any way to read all file by utl_file.fopen('ORALOAD', file_name,'r');
    We need to read all file info. because client claim for security issue and does not to overwirte output file name,
    we must find a way to read all file in output directory.
    Thanks for help!!!
    Jim

    If you use Chris Poole's XUTL_FTL package, I believe that contains functions that allows you to query the directory contents.
    http://www.chrispoole.co.uk/apps/xutlftp.htm
    Edited by: BluShadow on Jan 13, 2009 1:54 PM
    misread the original post

  • Open File Dialog shows all files

    I have searched the discussion boards and not found any solution to this problem. When I go to the File->Open dialog in Word, Matlab, etc, I see all files, even the hidden files. This is when "Enable" is set to filter on a certain file type. The files not matching the "Enable" filter are grayed out, but this still causes the list of files to be very long, even when only 1 or 2 files are of the right file type. Is there anyway to hide the files instead of just graying them out?
    Thanks,
    Dan

    I'm still having the same problem with 10.5.4. It is annoying to have to sort through many, many files that are grayed out just to find the one or two that a given app can actually open in the folder.

Maybe you are looking for