Java文件对象创建目录和文件

来源:互联网 发布:mac防蓝光 编辑:程序博客网 时间:2024/05/05 09:24

   在java中貌似不能同时创建目录和文件,需要分布创建,即不能在创建目录的时候,同时创建该目录下的文件,如果要创建的话,需要分两步,下边是创建的代码

Java代码 
  1. try {  
  2.             String path = "D:/test/d.txt";  
  3.             File file = new File(path);  
  4.             if (!file.getParentFile().exists()) {  
  5.                 file.getParentFile().mkdirs();  
  6.             }  
  7.                           file.createNewFile();  
  8.                 FileWriter fw = new FileWriter(file);  
  9.                 fw.write("test");  
  10.                 fw.close();  
  11.         } catch (IOException e) {  
  12.             e.printStackTrace();  
  13.         }