Results 1 to 9 of 9
  1. #1
    Zxcvtypo is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default Recursively listing files in a directory accepted as a string??

    I need to write a program that uses a recursive method to list the files ( >= a specific size) in a given directory (as specified by user input through scanner)..then returns a vector that contains all the files which meet the size criteria...

    I get the basic idea of the whole thing but I just can't figure out how to search a file or directory that a user inputs rather than a file already specified in the program..can anyone lead me in the right direction??

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Create a File which names the starting directory from the string that the user inputs.

    Your program will have to clearly define (and implement) how the string will be interpreted: should be the name of the directory relative to the current working directory? or relative to the user's home directory? or specified absolutely? or something else?

  3. #3
    Zxcvtypo is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    So I would use:

    System.out.println("Enter a directory name:");
    String dirName = scanner.next();

    to accept the name of the directory

    And this to set 'dirName' to the new file?:

    File f = new File(dirName);

    and from then on 'f' would represent the directory that the user specified?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes. And the API documentation for new File() will tell you how the user's string will be interpreted.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I'm pretty sure you need to use double slashes(\\ or //) when you create your new File.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zxcvtypo View Post
    So I would use:

    System.out.println("Enter a directory name:");
    String dirName = scanner.next();

    to accept the name of the directory

    And this to set 'dirName' to the new file?:

    File f = new File(dirName);

    and from then on 'f' would represent the directory that the user specified?
    Don't you want to read directory names with spaces in it? e.g. "C:\Program Files" ... if you do don't use that scanner.next() call.

    kind regards,

    Jos

  7. #7
    Zxcvtypo is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Don't you want to read directory names with spaces in it? e.g. "C:\Program Files" ... if you do don't use that scanner.next() call.

    kind regards,

    Jos
    What should I use in place of that??

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Zxcvtypo View Post
    What should I use in place of that??
    See if Scanner has a method that will read a whole line of user input, spaces and all.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    And on the question of slashes: / is fine for Windows or Linux. \\ is used to represent a back slash in a character or string literal - it has nothing to do with the string that the user should enter to specify the starting directory.

Similar Threads

  1. Replies: 3
    Last Post: 08-28-2009, 03:51 AM
  2. (Recursively?) Getting Files in a Directory
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 05-21-2009, 08:37 AM
  3. Listing file system from root directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 04-18-2009, 10:03 AM
  4. Listing subdirectories/files with filter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:20 AM
  5. Listing subdirectories/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:19 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
  •