我正在尝试写入文件以创建 JAR。但我有问题NullPointerException
。我的文件在类路径中。
(我使用getClass()
是因为我将创建一个 JAR 文件。)
有什么理由我会在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();
}
}