File file = new File("D:/projects/tFile.txt") ;
file.createNewFile(); //Unhandled exception type IOException
FileOutputStream fout = new FileOutputStream(file); //Unhandled exception type FileNotFoundException
String s = "Cricket";
byte []b = s.getBytes();
fout.write(b);//Unhandled exception type IOException
fout.close();// Unhandled exception type IOException
这显示了FileNotFound Exception和IoException。
答案 0 :(得分:-1)
它要求您处理从您使用的方法抛出的异常。
将它们包围在try catch块中。
public void method(){
try {
File file = new File("D:/projects/tFile.txt") ;
file.createNewFile();
FileOutputStream fout = new FileOutputStream(file);
String s = "IPL is very entertaining tournament";
byte []b = s.getBytes();
fout.write(b);;
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 1 :(得分:-2)
执行得很好。可能是你D:/项目丢失了。 或者可能是由于例外
public static void main(String[] args) throws IOException {
File file = new File("D:/Test/tFile.txt") ;
file.createNewFile();
FileOutputStream fout = new FileOutputStream(file);
String s = "IPL is very entertaining tournament";
byte []b = s.getBytes();
fout.write(b);;
fout.close();
}