2013-08-15 2 views
2

BufferedReader를 사용하고 있으며 close() 메서드를 호출해도 Eclipse는 여전히 경고 메시지를 표시합니다. 잠시 전에 close() 호출을하면 Eclipse에서 경고를 표시하지 않지만 코드가 작동하지 않습니다. 내 코드에 오류가 있습니까? 아니면 다른 문제가 있습니까? 코드 :BufferedReader가 닫히지 않았습니다. 경고가 사라지지 않습니다.

Hashtable<String, Hashtable<String, Integer>> buildingStats = new Hashtable<String, Hashtable<String, Integer>>(); 
    try 
    { 
     BufferedReader br = new BufferedReader(new FileReader(new File("Assets/Setup/Buildings.txt"))); // Sets the buildings values to the values in Buildings.tx 
     String line; 
     int lineNum = 0; 
     while((line = br.readLine()) != null) 
     { 
      ++lineNum; 
      String[] values = line.split(","); 
      if (values.length != 3) 
       throw new Exception("Invalid data in Assets/Setup/Buildings.txt at line " + lineNum); 
      if (buildingStats.containsKey(values[0])) 
      { 
       buildingStats.get(values[0]).put(values[1], Integer.parseInt(values[2])); 
      } 
      else 
      { 
       buildingStats.put(values[0], new Hashtable<String, Integer>()); 
       buildingStats.get(values[0]).put(values[1], Integer.parseInt(values[2])); 

      } 

     } 
     br.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return buildingStats; 
+1

퍼팅 시도 br.close(); 'finally'블록에서 – redFIVE

+0

마침내 닫아야합니다. 그 행에 도달하기 전에 예외가 발생하면 어떻게 될까요? –

답변

5

당신은 너무 같은 마지막 방법에 넣어해야합니다

BufferedReader br = null; 
try { 
    br = new BufferedReader(new FileReader(new File("Assets/Setup/Buildings.txt"))); 
    // do things 
} catch (Exception e){ 
    //Handle exception 
} finally { 
    try { 
     br.close(); 
    } catch (Exception e){} 
} 

여전히 경고 시도 청소를 얻을 이클립스 프로젝트를 재 구축합니다.

+0

'close()'에 의해 던져진 예외를 삼키는 것은 나쁜 생각입니다. –

+0

@ Louis Wasserman 예외를 삼킴으로써 당신은 무엇을 의미합니까? – PenguinAgen

+0

@LouisWasserman 나는 닫힌 상태로 닫을 수 없거나 연결을 닫을 수없는 경우 항상 닫으려고 시도한다고 생각합니다. 다시 전화를 걸어도 동일한 오류가 발생합니다. – Brinnis

1

선언과 close() 호출 사이에 아무 것도 예외를 throw 할 수 있습니다.이 경우 사용자의 close()은 호출되지 않습니다. finally 블록에 넣어보세요.

관련 문제