您没有告诉编译器,如果文件不存在,则有可能抛出
FileNotFoundExceptiona
FileNotFoundException将被抛出。
试试这个
public static void main(String[] args) throws FileNotFoundException { File file = new File ("file.txt"); file.getParentFile().mkdirs(); try { PrintWriter printWriter = new PrintWriter(file); printWriter.println ("hello"); printWriter.close();} catch (FileNotFoundException ex) { // insert pre to run when exception occurs }}


