📄 findinfilesthread.java
字号:
try {
// Use a UnicodeReader to auto-detect whether this is
// a Unicode file.
// FIXME: Allow the user to specify the default
// encoding, instead of assuming system default,
// somehow.
BufferedReader in = new BufferedReader(
new UnicodeReader(temp));
textArea.read(in, null); // Clears all old text.
in.close();
} catch (IOException ioe) {
MatchData data = createErrorMatchData(fileFullPath,
"IOException reading file: " + ioe);
dialog.addMatchData(data);
continue;
} catch (OutOfMemoryError oome) {
MatchData data = createErrorMatchData(fileFullPath,
"OutOfMemoryError");
dialog.addMatchData(data);
// Bail out.
dialog.searchCompleted(
System.currentTimeMillis() - startMillis);
return null;
}
String buffer = textArea.getText();
// If we got some text out of the file...
if (buffer!=null) {
int bufferLength = buffer.length();
// If we're NOT doing a regular expression search...
if (useRegExpressions==false) {
// Since we're using getNextMatchPosImpl, we do
// this ourselves to keep from doing it every
// iteration in the loop below.
if (!matchCase)
buffer = buffer.toLowerCase();
// If they're doing a "whole word" search, we
// cache the size of the string they're searching
// for + 1 so we don't have to keep recalculating
// it each call to getNextMatchPosImpl.
int tempChange = 0;
if (wholeWord)
tempChange = searchString.length() + 1;
// Get the first match.
int pos = FindDialog.getNextMatchPosImpl(
searchString, buffer, 0, true,
matchCase, wholeWord, tempChange);
// Some stuff we'll use below.
String lineText = null;
Element element = null;
Element map = textArea.getDocument().
getDefaultRootElement();
int lineCount = map.getElementCount();
// Loop while we find matches.
int numMatches = 0;
while (pos != -1) {
numMatches++;
// Get the line the match was found in, and
// its start and end positions.
int line = -1;
int lineEnd = bufferLength-1;
try {
line = map.getElementIndex(pos);
element = map.getElement(line);
int start = element.getStartOffset();
lineEnd = (line==lineCount-1) ? element.getEndOffset()-1 :
element.getEndOffset();
// Tabs don't show up in list items.
lineText = textArea.getText(start, lineEnd-start).replaceAll("\t", " ");
} catch (BadLocationException ble) {
// Never happens.
}
// If we're interested in seeing each
// matching line, display this information.
if (matchingLines==true) {
dialog.addMatchData(new MatchData(
fileFullPath,
""+(line+1), lineText));
}
// Otherwise, change lineEnd to the end of
// the MATCH, not it's line. This way, we get
// an accurate count if multiple matches are
// on the same line.
else
lineEnd = pos + searchString.length();
// Get the next match position.
pos = FindDialog.getNextMatchPosImpl(
searchString, buffer, lineEnd, true,
matchCase, wholeWord, tempChange);
} // End of while (pos != -1).
// If we're interested just in the number of
// occurrences in each file, now's the time to
// display this information.
if (matchingLines==false && numMatches>0) {
MatchData data = new MatchData(
fileFullPath,
"--",
numMatches + occurrencesString);
dialog.addMatchData(data);
}
} // End of if (useRegExpressions==false).
// Otherwise, we're doing a regular expression search.
else {
Point regExPos = FindDialog.getNextMatchPosRegEx(
searchString, buffer, 0, true,
matchCase, wholeWord);
int numMatches = 0;
while (regExPos != null) {
numMatches++;
// Get the line(s) the match was found in,
// and its start and end positions.
int startingLine = -1;
int endingLine = -1;
String text = null;
int end = bufferLength - 1;
try {
startingLine = textArea.getLineOfOffset(regExPos.x);
endingLine = textArea.getLineOfOffset(regExPos.y);
// Start and end of the FIRST LINE in the match.
int start = textArea.getLineStartOffset(startingLine);
end = textArea.getLineEndOffset(startingLine);
text = textArea.getText(start, end-start);
} catch (BadLocationException ble) {
// Never happens.
}
// If we're interested in seeing each
// matching line, display this information.
if (matchingLines==true) {
boolean oneLine = startingLine==endingLine;
String lineString2 = oneLine ?
("" + (startingLine+1)) :
((startingLine+1) + "-" + (endingLine+1));
String textString = oneLine ? text
: (text + dialog.getBundle().getString("MultiLineMatch"));
MatchData data = new MatchData(
fileFullPath, lineString2, textString);
dialog.addMatchData(data);
}
// Otherwise, change end to the end of the
// MATCH, not its line. This way, we get an
// accurate count if multiple matches are on
// the same line.
else {
// regExPos.y is the end position, NOT the length.
end = regExPos.y;
}
// Get the next match position.
regExPos = FindDialog.getNextMatchPosRegEx(
searchString, buffer, end, true,
matchCase, wholeWord);
} // End of while (regExPos != null).
// If we're interested just in the number of
// occurrences in each file, now's the time to
// display this information.
if (matchingLines==false && numMatches>0) {
MatchData data = new MatchData(
fileFullPath,
"--",
numMatches + occurrencesString);
dialog.addMatchData(data);
}
} // End of else.
} // End of if (buffer!=null).
} // End of if (temp.isFile()).
// Otherwise, if the file is a directory and the user wants to
// search subdirectories...
else if (temp.isDirectory()) {
// Ignore this (sub)directory if the user doesn't want
// to search subdirectories.
if (!checkSubfolders) {
if (doVerboseOutput) {
MatchData data = createVerboseMatchData(
fileFullPath, dontSearchSubfoldersString);
dialog.addMatchData(data);
}
continue;
}
// Get the list of files in this directory.
File[] moreFiles = temp.listFiles();
if (moreFiles==null) {
// Should never happen (as dirs return empty arrays).
continue;
}
// If there were files in this subdirectory, add these
// files to the list of files to search.
if (moreFiles.length>0) {
List moreFilesList = Arrays.asList(moreFiles);
fileList.addAll(moreFilesList);
int moreFilesListSize = moreFilesList.size();
numFiles += moreFilesListSize;
if (doVerboseOutput) {
MatchData data = createVerboseMatchData(
fileFullPath, newFilesToExamineString +
": " + moreFilesListSize);
dialog.addMatchData(data);
}
}
// Otherwise, this subdirectory was empty.
else {
if (doVerboseOutput) {
MatchData data = createVerboseMatchData(
fileFullPath,
newFilesToExamineString + ": 0");
dialog.addMatchData(data);
}
}
} // End of else if (temp.isDirectory()).
} // End of for (int i=0; i<numFiles; i++).
dialog.searchCompleted(System.currentTimeMillis() - startMillis);
return null;
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -