File对象创建文件
- public boolean createNewFile(); 创建文件,如果存在这个的文件,就不创建了
- public boolean mkdir(); 创建文件夹, 如果存在这样的文件夹就不会创建了
- public boolean mkdirs(); 创建文件夹,如果父文件夹不存在,会帮你创建出来
public class TestIO {
public static void main(String[] args) {
//创建文件
File f = new File("C:\\Users\\CJ\\Desktop\\1.txt");
//判断路径或文件是否存在
if(f.exists() == false) {
try {
f.createNewFile();
System.out.println("创建成功");
}catch (Exception e) {
System.out.println(e.getMessage());
}
}else{
System.out.println("文件已创建");
}
}
编译结果:
创建成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tcZhJ85E-1597495765446)(C:\Users\CJ\Pictures\Saved Pictures\io流1.png)]