큰 텍스트 파일에서 문자열 일치 작업을 구현하고 싶습니다. 1. 영숫자가 아닌 모든 문자를 바꿉니다. 2. 텍스트 파일에서 특정 용어의 수를 계산합니다. 예를 들어 용어 "tom"과 일치시킵니다. 일치하는 대소 문자를 구분하지 않습니다 .so 용어는 "톰"내가 계산해야합니다. 그러나 내일이라는 용어는 계산해서는 안됩니다. 두 코드를 실행하여큰 텍스트 파일에서 자바 문자열이 일치합니다.
code template one:
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
} catch (FileNotFoundException e1) {
System.out.println("Not found the text file: "+inputFile);
}
Scanner scanner = null;
try {
while ((line = in.readLine())!=null){
String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
scanner = new Scanner(newline);
while (scanner.hasNext()){
String term = scanner.next();
if (term.equalsIgnoreCase(args[1]))
countstr++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
code template two:
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile));
} catch (FileNotFoundException e1) {
System.out.println("Not found the text file: "+inputFile);
}
Scanner scanner = null;
try {
while ((line = in.readLine())!=null){
String newline=line.replaceAll("[^a-zA-Z0-9\\s]", " ").toLowerCase();
String[] strArray=newline.split(" ");//split by blank space
for (int =0;i<strArray.length;i++)
if (strArray[i].equalsIgnoreCase(args[1]))
countstr++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
, 나는 다른 결과를 얻을, 스캐너는 권리를 얻을처럼 보인다는 없었나 큰 텍스트 파일, 스캐너는 후자보다 훨씬 더 느리게 실행됩니다. 이유를 말해주고 훨씬 더 효율적인 해결책을 제시 할 수있는 사람.