-1

I want to search for particular string inside all files in a Directory.

Ex: Search for "tiger" in path D:/test/chapters/

D:/test/chapters
           /chapter1.log
           /chapter2.log
           /chapter3.log  all these sub files under D:/test/chapters/ .

Sample code I have tried :

public class Example  {
    public Example() {
        super();
    }

    public int plugin_execute() {
        boolean  foundstring=false;
        try {
            File dir = new File("D:/test/chapters");
            String[] children = dir.list();
            if (children == null) {
                System.out.println("does not exist is not a directory");
            } else {
                for (int i = 0; i < children.length; i++) {
                    String filename = children[i];
                    System.out.println(filename);

                    if (filename !=null) {
                        foundstring = testString(filename, "tiger");
                        System.out.println("failed");
                    }      

                    //Search for entry in file

                    if (!foundstring) {
                        return //failuremsg
                    } else {
                        System.out.println("failed");
                        return //succes
                    }
                }
            }
            return 1;
        } catch (Exception e) {
            return //error mssg
        }
    }

    private boolean teststring(String filePath, String str) {
        BufferedReader br = null;  
        File file = new File(filePath);
        boolean result = false;
        if(!file.exists())
            return false;
        try {
            br = new BufferedReader(new FileReader(filePath));
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                if (sCurrentLine.contains(str))  {
                    result = true;
                    System.out.println(str);
                    System.out.println("Found entry ");
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
}

It only returns the output of last file, means if it search success in last file it return success otherwise failed. But I want success if string is found in first file. i.e chapter1 should return success, if not found in Chapter1 it should continue search in chapter2 ....

Please suggest how can I modify this code..

user092
  • 437
  • 2
  • 10
  • 26

2 Answers2

0

Problem: Simple mix-up with ! and true/false locations.

Solution: Change this

if (! foundString) 
{
    return // failuremsg
} 
else 
{
    System.out.println("failed");
     return // success
}

to

if (foundString) 
{
    // return success message

} 
else 
{
    // return failure message
}

Another problem I believe I see in your code is that the line foundstring = findString(filename, "tiger"); calls the method findString, whereas the other method you posted in your code is testString. I assume this is a name mix up.

Queue
  • 446
  • 7
  • 23
  • about mix up function name I agree ...While changing name i missed it, but how changing if(!foundstring) with if(foundstring) will solve my issue? – user092 Jun 29 '16 at 16:14
  • @user092 because at the point of reaching the beginning of the `if` statement, the variable `foundString` is set to true if the search string was found in the file. In the code you posted, `if( ! foundString)`, the `!` negates the value of `foundString`, changing it to the opposite of what you want. – Queue Jun 29 '16 at 16:16
  • I ran this code with the first file containing "Tiger" and it resulted in `return true` and the application ended. – Queue Jun 29 '16 at 16:18
0
public void listFiles(Path dir , String text)
{
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir))
    {
        for (Path path : directoryStream)
        {
            if (Files.isRegularFile(path) && Files.isReadable(path))
            {
                //this.findString(path, text);
            }
        }
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
}

private boolean findString(Path file, String text)
{
    //Your implementation
    return true;
}