2013-11-25 2 views
-1

안녕하십니까/저는 java에 익숙하지 않고 txt 파일을 파싱하고 내용을 한 줄씩 스캔하는 프로그램을 만들려고합니다. 글쎄, 구걸에 어떤 객체 또는 클래스없이 메인 프로그램을 설계하고 괜찮 았어. 그런 다음 주 기능의 콘텐츠로 새 클래스를 디자인하려고했지만 컴파일에 실패하고 많은 예외 오류가 발생합니다. 여기에 코드입니다 :다른 기능의 Java 예외

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 


public class Reader { 


Reader(String file) throws Exception{ 
    Entry test= new Entry(); 

    File f= new File("C:/Users/Mario/Documents/Visual Studio 2013/Projects/Entry/Entry/a.txt"); 
    System.out.print(f.exists()); // true is printed on the screen when this was at main 

    FileInputStream fstream = new FileInputStream("C:/Users/Mario/Documents/Visual Studio 2013/Projects/Entry/Entry/a.txt"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 

    String strLine; 
    String tag=""; 
    String date=""; 
    int id; 
    String title="",description="",reference=""; 

    while ((strLine = br.readLine()) != null){ 

     System.out.print(strLine+ "\n"); 
     if(strLine.length()>2 && strLine.substring(0,3).equals("I1:")){ 

      System.out.print("id detected\n"); 
      id=-1; 
      strLine = br.readLine(); 
      id=Integer.parseInt(strLine); 
      //System.out.print("ID:"+id+"\n"); 
      test.set_id(id); 
      continue; 
     } 

     if(strLine.length()>2 && strLine.substring(0,3).equals("T1:")){ 
      System.out.print("title detected\n"); 
      title=""; 
      strLine = br.readLine(); 
      title=strLine; 
      //System.out.print("Title:"+title+"\n"); 
      test.set_title(title); 
      continue; 
     } 

     if(strLine.length()>2 && strLine.substring(0,3).equals("D1:")){ 
      System.out.print("description detected\n"); 
      StringBuilder descrp=new StringBuilder(); 
      description=""; 
      while(true){      
       strLine = br.readLine(); 
       if(strLine.equals("D2:")) break; 
       else descrp.append(strLine); 
       continue; 
      } 
      description=descrp.toString(); 
      //System.out.print(description); 
      test.set_description(description); 
     } 

     if(strLine.length()>2 && strLine.substring(0,3).equals("D3:")){ 
      System.out.print("DATE"+date); 
      strLine = br.readLine(); 
      date=strLine; 

      test.set_date(date); 
     } 

     if(strLine.length()>2 && strLine.substring(0,3).equals("R1:")){ 

      strLine = br.readLine(); 
      reference=strLine; 
      System.out.print("Reference"+reference); 
      test.set_reference(reference); 
     } 
     br.close(); 
    } 
} 
} 

이러한 내가받을 오류입니다 : 나 한테 무슨 혼란을하는 것은 내가 코드 된 파일을 변경할 때이 오류를받은 사실은

true 
Exception in thread "main" java.io.IOException: Stream closed 
    at java.io.BufferedReader.ensureOpen(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at Reader.<init>(Reader.java:28) 
    at FileReader.main(FileReader.java:14) 

입니다. 컴파일하기 전에 오류가 발생하기 전에 프로그램이 정상적으로 작동했습니다. 새 파일 (Reader.java)은 main 함수가있는 파일과 같은 디렉토리에 있습니다. 나는 Eclipse IDE에서 일한다. advnace에서 들으

편집 : 함수에서 던져 예외를 추가 따라서 br.close() 문이 잘못된 장소에있는 콘솔 출력

+5

그래서 ... 왜 그 모든 것을 생성자에서하고 있습니까? 또한 예외는 매우 간단합니다. 파일을 사용하고 있기 때문에 파일의 사용을'try ... catch' 블록으로 감싸거나 메소드의 서명에'throws IOException'을 추가합니다 (,이 경우 생성자가됩니다). – Makoto

+0

다른 파일에서이 코드를 이동하기 전에 예외가없는 이유를 알 수 없습니다. 본부에는이 문제들 중 아무 것도 없었다. – JmRag

+0

나는'main'이'throws' 또는'try ... catch' 블록을 가지고 있다고 내기를 기꺼이합니다. 만약 그렇지 않다면, 당신의 주된 방법은 컴파일되지 않을 것입니다. – Makoto

답변

2

을 변경했습니다. 루프가 끝날 때 닫혀 있으므로 루프의 시작 부분에서 더 많은 정보를 얻으면 스트림이 닫힙니다. br.close을 루프 밖으로 이동하면됩니다.

+0

예, @peeskillet이 댓글에서 나에게 말했습니다. 나는 C/C++를 잘 알고 있으며 세분화 오류와 같은 오류가 발생할 것이라고 생각했습니다. Thx 도움! – JmRag