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 5 of 5

Thread: Seraching through files in a folder for a pattern match inside the files.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Seraching through files in a folder for a pattern match inside the 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:

    package searchfile;
     
    import java.io.*;
    import javax.swing.*;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.charset.Charset;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.text.DateFormat;
    import java.util.Date;
    import java.util.ArrayList;
    import java.util.List;
     
    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 only included the code I am having issues with and removed the rest. Any help would be greatly appreciated.
    Last edited by dazzabiggs; May 2nd, 2011 at 08:15 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Seraching through files in a folder for a pattern match inside the files.

    I've added highlight tags, but that is way too much code for most of us to wade through. I suggest you throw together an SSCCE that demonstrates the problem. I only needs to be a few lines long; you can hardcode the pattern to search and output it to the command prompt.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Seraching through files in a folder for a pattern match inside the files.

    I've now removed the extra code and condensed it down to only the bits I have an issue with. Cheers.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Seraching through files in a folder for a pattern match inside the files.

    I'd love to help, but that's not an SSCCE because it contains references to stuff that you don't define. Like I said, a simple main method that searches for a hardcoded pattern and outputs to the command line in a barebones program would be best.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Seraching through files in a folder for a pattern match inside the files.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/43383-searching-through-folder-find-text-inside-all-files.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to create or edit .ser files in Jar files
    By xbill in forum Java IDEs
    Replies: 1
    Last Post: May 18th, 2011, 05:15 AM
  2. Not able to put some files inside Zip file
    By chinnu in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 23rd, 2011, 05:17 AM
  3. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM
  4. Listing files in a folder in a .jar
    By kulan8 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 22nd, 2010, 08:18 AM
  5. need help on jar files
    By kaka09 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 13th, 2010, 09:30 AM