2013-01-19 2 views
0

JAR을 작성하기 위해 파일에 쓰려고합니다. 그러나 NullPointerException에 문제가 있습니다. 내 파일은 Classpath에 있습니다.NullPointerException JAR 작성 파일에 쓰는 중

(I는 JAR 파일을 생성 할 수 있기 때문에 내가 getClass()를 사용합니다.)

어떤 이유로 내가 여기 NullPointerException을 얻을 것? 당신의 도움을 주셔서 감사합니다. 여기

코드입니다 :

package Library; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.URISyntaxException; 

public class WriteJARSample { 
public WriteJARSample() throws URISyntaxException, FileNotFoundException, FileNotFoundException, IOException{ 
    write(); 
} 
public void write() throws URISyntaxException, FileNotFoundException, IOException{ 
    try{ 
     File Mf=new File(getClass().getClassLoader().getResource("AllBookRecords.txt").toURI()); 
     File Tf=new File(getClass().getClassLoader().getResource("Boutput.txt").toURI()); 
     BufferedReader Bbr=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("AllBookRecords.txt"))); 
     PrintWriter Bpw=new PrintWriter(Mf); 
     String Bs; 
     while((Bs=Bbr.readLine()) != null){ 
       Bpw.println(Bs); 
     } 
     Bpw.close(); 
     Bbr.close(); 
     Mf.delete(); 
     Tf.renameTo(Mf); 
    } 
    catch(IOException ioe){ 

    } 
    catch(URISyntaxException urise){ 

    } 
} 
public static void main(String[] args) throws URISyntaxException, FileNotFoundException, IOException{ 
    new WriteJARSample(); 
} 
} 
+0

NullPointerException이 발생하는 행은 무엇입니까? – MrSmith42

+0

"File Tf = ..."줄에서 – Sajad

+0

getClass(). getClassLoader(). getResource ("Boutput.txt"가'null '을 반환합니다.) – MrSmith42

답변

3

getClass().getResourceAsStream("AllBookRecords.txt")는 "AllBookRecords.txt"의 사전 접근을 위해 다른, 어쩌면 그것은 또한해야한다 :

getClass().getClassLoader().getResourceAsStream("AllBookRecords.txt") 

하거나

getClass().getResourceAsStream("/AllBookRecords.txt") 

getClass().getResource(AsStream)은 클래스 패키지 (하위 디렉토리)를 기준으로 작동합니다.

+0

+ 1. 나는 마지막 옵션이 (이 경우에는) 작동해야한다고 생각한다. –

관련 문제