当在文件中搜索字符串“$ DATA ASCII”时,我的findWithinHorizon方法返回null。相同的方法可以找到其他字符串,例如“Array Start”。
这是我搜索文件的代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class SearchingFilesMain {
public static void main(String[] arg) {
String checker;
String pattern1= "$DATA ASCII";
try {
Scanner scan = new Scanner(new BufferedReader(new FileReader(
"C:\\Users\\User\\Desktop\\asciiTracks.txt")));
checker = scan.findWithinHorizon(pattern1,0);
System.out.println(checker);
} catch (FileNotFoundException e) {
System.out.println(e.getStackTrace());
}}}
这是文件的一部分,如记事本++中所示:
$DATA ASCII
$DATA_INFO
Array Start = 0 * 126
$PROCESSING_INFO
Generation Time = 13:53:42
$GENERAL
Tx Time = 450.079
Tx Time Abs = 1080309222.55633
Tx Clock = 13:53:42
Cerb Tracker Whole Time = 0.015
Num contacts = 699
$DATA ASCII
$DATA_INFO
Array Start = 0 * 127
$PROCESSING_INFO
Generation Time = 13:53:45
$GENERAL
Tx Time = 453.219
Tx Time Abs = 1080309225.69626
Tx Clock = 13:53:45
Cerb Tracker Clutter processing = 0.016
Cerb Tracker Whole Time = 0.016
Num contacts = 666
$DATA ASCII
我不知道为什么那么方法不能搜索这段代码来找到“$ DATA ASCII”,有人能想出任何理由吗?
答案 0 :(得分:1)
$
是正则表达式中的元字符,用于表示String
的结尾。它需要被转义
String pattern1 = "\\$DATA ASCII";