Results 1 to 7 of 7
  1. #1
    pooja123 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default how to get file names from a directory or package in eclipse ?

    hi ,

    I am trying to write a small code here where in i want to get file names from a directory..
    here is the code i am trying..

    import java.io.*;
    public class GetFileList
    {
    public static void main(String args[]){
    File file = new File("home/");
    File[] files = file.listFiles();
    for (int fileInList = 0; fileInList < files.length; fileInList++)
    {
    System.out.println(files[fileInList].toString());
    }
    }
    }

    but it gives me an error saying
    Exception in thread "main" java.lang.NullPointerException
    at GetFileList.main(GetFileList.java:7)

    (1) could anyone tel me, y is it so ? and what am i supposed to add now to get the file names from the directory..

    (2) and i also want to know how to work in the same way in eclipse , so as to get file names from a particular package, instead of a directory...

    Thanking you in advance

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    File file = new File("home/");

    remove the forward slash '/'

    the name of the directory is "home" not "home/"


    "home/" becomes "home/null" as if the compiler expected another folder name:

    "home/subdirectory"

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Bad news: use Code-tags and indent when you post code.

    Good news: @(1) I've run your code and it runs fine. The only thing I adapted is the path of course.

    @(2) A package is a directory, so the same way.
    (it might help to use a little diagnostic method like:
    Java Code:
    	public static void testFile( String filePathAndName){
    		File file =new File( filePathAndName);
    		String path = file.getAbsolutePath();
    		System.out.println( "File: " + filePathAndName + "\n" + "AbsPath: " + path );
    	}
    )
    Last edited by Jodokus; 04-16-2011 at 04:51 PM.

  4. #4
    pooja123 is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    hey thanks a lot to u both :) it works now with the same code i had given above.. it was only my path problem.. and it works the same way with the package too..

    but now i just hav a small question again.. i get the list of file names with the path..
    but i want only the file names..
    i get like home/blabla/bla/fileName.txt.. i want only "fileName.txt" without the path..

    could u tel me how can i do that now with the code i hav given above.. i mean how can i just avoid the path...?

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I'm so sorry. I'm getting into trouble on this forum when spoonfeeding.
    I can't imagine that you can't find out with what you have already.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Pooja, instead of printing File as String:
    Java Code:
    System.out.println(files[fileInList].toString());
    You can use the File object's getName() method to get just the file name like this:
    files[fileInList].getName()

    Please refer to the documentation for more information
    File (Java 2 Platform SE v1.4.2))

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 11:22 AM
  2. Accessing Package from a Different Directory
    By rameshiit19 in forum New To Java
    Replies: 11
    Last Post: 09-20-2010, 10:40 AM
  3. Replies: 0
    Last Post: 04-12-2009, 04:06 AM
  4. Replies: 8
    Last Post: 02-20-2009, 01:40 PM
  5. Adding file names(Help!!)
    By Gambit17 in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 08:26 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
  •