练习 - 写入数据到文件

    如果是写入数据到d:/xyz/lol2.txt,而目录xyz又不存在的话,就会抛出异常。
    那么怎么自动创建xyz目录?

    1. public static void main(String[] args) {
    2. try {
    3. File f = new File("f:/lolfolder2/lol2.txt");
    4. if (!f.getParentFile().exists()){
    5. f.getParentFile().mkdirs();
    6. f.createNewFile();
    7. }
    8. FileOutputStream fos = new FileOutputStream(f);
    9. fos.write(69);
    10. fos.close();
    11. }catch (IOException e){
    12. e.printStackTrace();
    13. }
    14. }