NullPointerException
ファイルが見つからない場合、どうすれば印刷できますか? を使用してメッセージを表示したいのですJOptionPane
が、うまくいきませんでした。
私は次のことを試しました:
public void readTextFile() {
FileInputStream fs = null;
try {
URL file2 = getClass().getResource("/ReadWriteFile/Student.txt");
String file_path=file2.getPath();
file_path=file_path.replaceFirst("/","");
File file = new File(file_path);
fs = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fs));
try {
String line = reader.readLine();
while(line != null){
System.out.println("line=="+line);
line = reader.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"The file you trying to reach is not found");
Logger.getLogger(Read_WriteToFile.class.getName()).log(Level.SEVERE, null, ex);
}
}