Results 1 to 4 of 4
  1. #1
    dazzabiggs is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Unhappy Searching through folder to find text inside all files

    I am new to java and I am having a problem with some code. I am trying to create a java GUI whereby users enter a piece of text to search and a folder to search. The code will then search iteratively through each file for the text and append the results in a jtextarea. My search works for just one file if I exclude the loop through the files. When looping it does not produce any errors but I receive no output. My code is:

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    //Runs the method to search through the files in a folder, this does not work yet!
    DoSearchFolder();
    } 
    /*Method to search iteratively through a folder and check all files for
    text search criteria - This is not working but yet produces no errors! 
    */
    public void DoSearchFolder(){
    
    try {
    
    /*This bit appears to work as I get the message if the directory
    does not exist */
    File folder = new File(fileLocation.getText());
    String[] directory = folder.list();
    if (directory == null){
    JOptionPane.showMessageDialog(null, "This is not a directory or the directory does not exist");
    } else {
    /*From hear is where the problems seems to be, if only I got 
    errors! */
    for (int i=0; i<directory.length; i++){
    String listFilenames = directory[i];
    
    
    Pattern pattern =
    Pattern.compile(searchText.getText());
    
    Matcher matcher =
    pattern.matcher(fromFile(listFilenames));
    
    boolean found = false;
    
    while (matcher.find()) {
    String group = matcher.group();
    int start = matcher.start();
    int end = matcher.end();
    String endMatch = Integer.toString(end);
    String startMatch = Integer.toString(start);
    
    outputScreen.append("I found the text " + group + " starting at character " + startMatch + " and ending at character " + endMatch + " " + "from file " + listFilenames);
    
    found = true;
    
    
    }
    
    if(!found){
    
    JOptionPane.showMessageDialog(null, "No Match Found");
    
    }}}
    
    
    } catch (IOException e) {
    
    }}
    I have just included the code that matters. Any help would be greatly appreciated.

    Cheers

    Darren

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    I guess you didn't like the advice I already gave you.
    Crossposted: Seraching through files in a folder for a pattern match inside the files.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    dazzabiggs is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Actually Kevin I did not know that java-forums.org and javaprogrammingforums.com were in any way linked. I have had of known that I would not have posted the same thing on both :).

    I will check out your post on the other forum.

    Thanks.

    Darren

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by dazzabiggs View Post
    Actually Kevin I did not know that java-forums.org and javaprogrammingforums.com were in any way linked. I have had of known that I would not have posted the same thing on both :).

    I will check out your post on the other forum.

    Thanks.

    Darren
    They aren't linked; I just happened to be reading both. The point is (as the link on the other forum tells you), the problem with crossposting is that people have no idea what help you've already received- so they often wind up repeating advice you heard days ago, or attempting to help you even after the problem is solved. People don't like wasting their time, but simply saying "I also crossposted this here" is usually enough.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 9
    Last Post: 04-12-2011, 07:26 PM
  2. Searching directories for folders and .txt files
    By XDrew574X in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 09:41 PM
  3. java searching from many files
    By manish250 in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 07:50 AM
  4. Replies: 0
    Last Post: 10-28-2010, 12:46 PM
  5. Need to find large files and folder on the PC. What app needed?
    By Cleaner007 in forum Reviews / Advertising
    Replies: 1
    Last Post: 09-30-2008, 07:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •