Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trying to read a directory recursively and match a String with the files present and return the file names matching the String.

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Trying to read a directory recursively and match a String with the files present and return the file names matching the String.

    I was doing a program that searches a directory recursively and matches a String "Claims" with the file names.
    If the files are found with name starting from Claims they shall be shown in the output.
    I am posting my code below.

    import java.io.File;

    public class FileSearchRecursively {
    public static void main(String[] args){
    FileSearchRecursively f=new FileSearchRecursively();
    f.listFile("D:/Abhishek/src", "Claims" );
    }

    public void listFile(String pathname,String searchKey) {
    File f = new File(pathname);
    File[] listfiles = f.listFiles();
    for (int i = 0; i < listfiles.length; i++) {
    if (listfiles[i].isDirectory()) {
    File[] internalFile = listfiles[i].listFiles();
    for (int j = 0; j < internalFile.length; j++) {
    String val=internalFile[j].getAbsolutePath();
    if (internalFile[j].isDirectory()) {
    String name = internalFile[j].getAbsolutePath();
    listFile(name,searchKey);
    }else
    {
    if(val.equals(searchKey));
    }
    }
    }
    else {
    System.out.println(listfiles[i]);
    }

    }

    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Trying to read a directory recursively and match a String with the files present and return the file names matching the String.

    Wrap your code in code tags. Do you have a question, or is that just an example for fragile code?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to read a directory recursively and match a String with the files present and return the file names matching the String.

    I have given my code.
    Its just traversing all the files but not returning the files that matches with the string "Claims".
    Can you help me with that only. what modifications are needed to acheive the required output?

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Trying to read a directory recursively and match a String with the files present and return the file names matching the String.

    I have no idea what the required output should be, I have no idea what you currently get and you've never asked a question.
    Wrap your code in code tags and read this: http://www.javaprogrammingforums.com...wbie-tips.html

Similar Threads

  1. How to read a String .dat file into an array
    By Wallatrix in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 5th, 2013, 02:55 PM
  2. Fastest way to read and search a string in a large file using java
    By patilsn_jay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 09:29 AM
  3. HW-how to read a string from a text file
    By yaboibangz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 09:34 AM
  4. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  5. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM

Tags for this Thread