2009-06-10 1 views
0

로그 파일을 구문 분석하는 Java 클래스를 작성 중입니다. 컴파일 된 .class 파일은 배포 및 호출을 위해 타사 모니터링 플랫폼 (eG)에로드해야합니다. 불행히도 타사 플랫폼에서는 .class 파일 하나만 업로드 할 수 있습니다.익명 또는 내부 클래스를 단일 java .class 파일로 컴파일 할 수 있습니까?

내 현재 구현은 파일 마스크 (* CJL * .log)를 준수하는 폴더에서 '최신'파일을 찾고 두 개의 익명 클래스를 사용하여 디렉토리 목록을 필터링하고 다른 클래스는 ModifiedDt를 기반으로하는 파일 목록 컴파일 할 때 배포 할 수없는 .class 파일 (Monitor.class, Monitor $ 1.class, Monitor $ 2.class)이 3 개 있습니다.

타사 모니터링 플랫폼에 배포하기 위해 익명 클래스를 단일 .class 파일로 컴파일 할 수 있습니까?

일러스트레이션을위한 '최근 파일 찾기'기능의 코드를 첨부했습니다.

private String FindLatestFile(String folderPath) { 
    FilenameFilter filter = new FilenameFilter() { 
     public boolean accept(File dir, String name) { 
      if (name.endsWith(".log") 
        & name.contains("CJL")) 
       return true; 
      else 
       return false; 
     } 
    }; 

    File dir = new File(folderPath); 

    File[] files = dir.listFiles(filter); 

    if (files.length > 0) { 
     Arrays.sort(files, new Comparator<File>() { 
      public int compare(File f1, File f2) { 
       return Long.valueOf(f1.lastModified()).compareTo(
         f2.lastModified()); 
      } 
     }); 

     File newest = files[files.length - 1]; 

     return newest.toString; 
    } else { 
     return ""; 
    } 
} 

내가 자신을 분류하지만이 확대됨되지 않습니다 걱정/나열하는 원시 파일을 받고 필터를 수행하여 이것을 '바보'방법을 수행 할 수 있습니다 가정합니다.

어떤 아이디어?

마이클

답변

4

아니요 afaik. jar도 사용할 수 없다고 가정합니다.

해결 방법은 내부 클래스의 필요성을 제거하기 위해 클래스가 두 인터페이스를 구현하도록하는 것입니다.

class MyClass implements FilenameFilter, Comparator<File> { 
    ... 

    public boolean accept(File dir, String name) { 
     if (name.endsWith(".log") & name.contains("CJL")) 
       return true; 
     else 
       return false; 
    } 

    public int compare(File f1, File f2) { 
     return Long.valueOf(f1.lastModified()).compareTo(
       f2.lastModified()); 
    } 

    private String FindLatestFile(String folderPath) { 

     File dir = new File(folderPath); 

     File[] files = dir.listFiles(this); 

     if (files.length > 0) { 
      Arrays.sort(files, this); 

      File newest = files[files.length - 1]; 

      return newest.toString; 
     } else { 
      return ""; 
     } 
} 
+0

아하! 훌륭한! 나는 그런 생각을 전혀하지 않았다. 감사 :) –