로그 파일을 읽고 특정 문자열이 표시되는 횟수를 계산하는 프로그램을 작성 중입니다. 나는 수동으로 문자열을 키워드로 입력하려고했으나 너무 많이 있기 때문에 로그 파일을 검색하는 것이 더 좋을 것이라고 판단하고 "ua"를 만나면 "ua"에서 새 문자열을 만들어야합니다. 줄의 끝을 해시 맵에 추가하고 해당 특정 문자열 ("ua"로 시작하는 모든 문자열)에 대한 수를 늘립니다. 해시 맵에 새 문자열을 추가하는 방법을 알아낼 수 없습니다. 이것은 내가 지금까지 가지고있는 것이다.새로운 문자열을 해시 맵에 추가하기 자바
public class Logs
{
public static void main(String args[]) throws IOException
{
Map<String, Integer> dayCount = new HashMap<String, Integer>();
for (String str : KeyWords)
{
dayCount.put(str, 0);
}
File path = new File("C:\\P4logs");
for(File f: path.listFiles())
{ // this loops through all the files + directories
if(f.isFile())
{ // checks if it is a file, not a directory.
try (BufferedReader br = new BufferedReader(new FileReader(f.getAbsolutePath())))
{
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null)
{
boolean found = false;
for (String str : DayCount.keySet())
{
if (sCurrentLine.indexOf(str) != -1)
{
DayCount.put(str, DayCount.get(str) + 1);
found = true;
break;
}
}
if (!found && sCurrentLine.indexOf("ua, ") != -1)
{
System.out.println("Found an unknown user action: " + sCurrentLine);
DayCount.put(key, value) //not sure what to put here
}
}
}
for(String str : KeyWords)
{
System.out.println(str + " = " + DayCount.get(str));
}
}
}
}
}
무엇을 넣어야할지 모르시겠습니까? .... 다른 곳에 두는 것만 넣으면됩니다. – Colleen
** DayCount ** 란 무엇입니까? 어떻게 그리고 어디에서 정의됩니까? –
'DayCount'는 표준'HashMap'입니까? – iamnotmaynard