我有一個小型的java程序,用於搜索特定字符串文件夾中所有* .txt文件的內容。我的問題搜索完全匹配正在返回子字符串。
例子:
- 輸入字符串發現:
- 輸入字符串發現:6570
- 輸入字符串發現:6570
發現2 kyle.txt時間(s) !
在kylezz.txt找到2時間!
按任意鍵繼續。 。 。
問題:
它正在尋找6570,但是我攝像結果與該字符串這樣的值:
11111116570111111 657011111 111116570 6570
問:我要搜索只有一個確切的字符串,例如:「6570」。我怎麼才能讓它返回只有 6570的確切值?我不想在開始或結束時添加任何額外的字符,只有確切的值。
這裏是我的代碼:
import java.io.*;
import java.util.*;
public class FileScanner {
public static void main(String args[]) {
System.out.print("Enter string to find: ");
Scanner sc = new Scanner(System.in);
find(sc.nextLine());
}
public static void find(String delim) {
File dir = new File("files");
if (dir.exists()) {
String read;
try {
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File loaded = files[i];
if (loaded.getName().endsWith(".txt")) {
BufferedReader in = new BufferedReader(new FileReader(
loaded));
StringBuffer load = new StringBuffer();
while ((read = in.readLine()) != null) {
load.append(read + "\n");
}
String delimiter[] = new String(load).split(delim);
if (delimiter.length > 1) {
System.out.println("Found "
+ (delimiter.length - 1) + " time(s) in "
+ loaded.getName() + "!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("error: dir wasn't found!");
}
}
}
謝謝你們我真的希望你能幫助我與我的編程問題。我一直試圖修復它大約一個月,現在我正在尋求幫助。
格式化你的源代碼或者它是不可能讀的 – rachvela 2009-12-09 19:27:30