2011-04-12 4 views
0

소스 코드에서 읽을 수있는 java에서 전 처리기를 만들려고합니다. 코드를 모두 하나의 문자열로 읽으려고했습니다.Java : 파일 판독기, 구분 기호로 구분 기호 도움말

질문 : < < < >>> 중간에있는 문자열을 일종의 자체 배열 목록에 어떻게 추가합니까?

public class processLines { 
public void pLine (String FileName)throws IOException{ 
    Scanner scanner = null; 
    try{ 
     scanner = new Scanner(new BufferedReader(new FileReader(FileName))); 
     while (scanner.hasNext()) { 
      String Line = ""; 
      String LineB = ""; 
      String LineC = ""; 
      ArrayList<String> inside = new ArrayList<String>(); 
      Line = Line + scanner.next()+ " "; 
      System.out.println("outside token: "+ Line); 
      StringTokenizer token = new StringTokenizer(Line); 
      while(token.hasMoreTokens()&& token.nextToken() != null){ 
       LineB = Line; 
       if(LineB.contains("<<<")){ 
        if(!LineB.contains(">>>")){ 
         LineC = LineC + scanner.next()+ " "; 
         inside.add(LineC); 
         System.out.println("LineC: " + LineC); 
         System.out.print(inside); 
        } 
         if(scanner.next(">>>") != null){ 
          Line = scanner.next(); 
          System.out.println("Line INside:" + Line); 
         } 
       } 
      } 
     } 
    } 
    finally { 
     if (scanner != null) { 
      scanner.close(); 
     } 
    } 
} 

}

텍스트 파일 소스 코드는 "모 < < < 모 래리 곱슬 >>> 래리"한 줄에 모두 포함되어 있습니다. 이 코드는 < < < >>>의 중간에 하나의 이름 만 있으면 작동하지만 더 추가하면 오류가 발생합니다. 외부 토큰 : 발생

오류 메시지 모

외부 토큰 : < < < LineC : 모 [모] 글 예외 "메인"java.util.InputMismatchException java.util.Scanner에서 proProcess.main에서 processLines.pLine (processLines.java:26) 에서 java.util.Scanner.next (알 소스)에 java.util.Scanner.next (알 소스)에 .throwFor (알 소스) (proProcess.java:14)

답변

3

문제는 다음 토큰이 다음 토큰이 아닐 때 닫는 wickets을 찾고 있다는 것입니다. 논리를 깨뜨려서 입력 파일에서 한 줄씩 읽을 수 있습니다. 한 행에 개찰구 구분 기호가있는 경우 다른 스캐너를 사용하여 개찰구간에 내용을 분할합니다. 여기에 코드가 있습니다. 이것은 개찰구가없는 wickets 및 lines 내부의 0 개에서 많은 토큰에 적용됩니다.

public static void pLine (String FileName)throws IOException{ 
Scanner scanner = null; 
try{ 
    scanner = new Scanner(new BufferedReader(new FileReader(FileName))); 
    String line; 
    ArrayList<String> inside; 
    Scanner inner; 
    int start; 
    int end = 0; 
    while (scanner.hasNextLine()) { 
      line = scanner.nextLine(); 
      inside = new ArrayList<String>(); 
      start = line.indexOf("<<<", end);  
      end = line.indexOf(">>>", start+1); 
      if (end > start) { 
       inner = new Scanner(line.substring(start +3, end)); 
       while (inner.hasNext()) { 
        inside.add(inner.next()); 
       } 
      } 
      System.out.println("inside : " + inside); 
     } 
    } 
    catch (Throwable t) { 
     t.printStackTrace(); 
    } 
    finally { 
     scanner.close(); 
    } 
} 
관련 문제