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.