JAVA之File类创建对象构造函数传参数需要注意的几点(二)
System.out.println("文件夹名称:" + str);
System.out.println("是否是目录:" + file1.isDirectory());
}
else
{
try{
file1.mkdirs();
System.out.println("创建文件夹成功");
}catch(Exception e)
{
System.out.println("异常");
}
}
File file = new File(file1, "helloworld.txt");
if(file.exists())
{
System.out.println("==============================");
String str = file.getName();
long l = file.length();
String str1 = file.getAbsolutePath();
String str2 = file.getParent();
boolean b1 = file.isFile();
boolean b2 = file.isDirectory();
System.out.println("长度:" + l);
System.out.println("文件名称:" + str);
System.out.println("绝对路径:" + str1);
System.out.println("父路径:" + str2);
System.out.println("是否是文件:" + b1);
System.out.println("是否是目录:" + b2);
}
else
{
try{
file.createNewFile();
System.out.println("文件创建成功");
}catch(Exception e)
{
System.out.println("异常");
}
}
}
}
输出结果:
文件夹名称:src
是否是目录:true
==============================
长度:0
文件名称:helloworld.txt
绝对路径:G:\text1\src\helloworld.txt
父路径:G:\text1\src
是否是文件:true
是否是目录:false