创建文件

public static void main(String[] args) throws IOException {
File file = new File("D:\\io\\myTest.txt");      //文件地址
if(file.exists()){                     //文件已经存在
System.out.println("文件已经存在");
System.exit(-1);
}
file.getParentFile().mkdir();               //创建父文件夹
try{
file.createNewFile();                  //创建文件
}catch (IOException e){
e.printStackTrace();
System.exit(-1);
}
System.out.println("文件已经创建成功");
System.out.println(file.getAbsolutePath());

}
posted @ 2021-01-29 16:39  __sunshine  阅读(211)  评论(0)    收藏  举报