CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2011

    Angry Searching through a folder and checking all files for text inside.

    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!
    /*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 =
    Matcher matcher =
    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;
    JOptionPane.showMessageDialog(null, "No Match Found");
    } catch (IOException e) {
    I have only included the code I am having
    I have only included the code I am having issues with and removed the rest. Any help would be greatly appreciated.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999

    Re: Searching through a folder and checking all files for text inside.

    Most of us will generally move on rather than try to figure out unformatted code - indentations are important. An SSCCE is also helpful in these cases.

    Don't you hate code that's not properly indented? Making it [indenting] part of the syntax guarantees that all code is properly indented...
    G. van Rossum(designer of the Python)
    Last edited by dlorde; May 2nd, 2011 at 07:10 PM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.