Results 1 to 4 of 4
- 05-02-2011, 01:46 PM #1
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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:
I have just included the code that matters. Any help would be greatly appreciated.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) { }}
Cheers
Darren
- 05-02-2011, 02:14 PM #2
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!
- 05-02-2011, 09:40 PM #3
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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
- 05-03-2011, 01:20 PM #4
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
-
Getting text from inside a jeditorpane inside a jtabbedpane
By captain alge in forum New To JavaReplies: 9Last Post: 04-12-2011, 07:26 PM -
Searching directories for folders and .txt files
By XDrew574X in forum New To JavaReplies: 1Last Post: 03-29-2011, 09:41 PM -
java searching from many files
By manish250 in forum Advanced JavaReplies: 0Last Post: 12-31-2010, 07:50 AM -
Does eclipse supports multiple sub folder inside source folder
By anantth in forum EclipseReplies: 0Last Post: 10-28-2010, 12:46 PM -
Need to find large files and folder on the PC. What app needed?
By Cleaner007 in forum Reviews / AdvertisingReplies: 1Last Post: 09-30-2008, 07:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks