File使用
package cn
.test
;
import java
.io
.File
;
import java
.io
.IOException
;
import java
.util
.Date
;
public class TestFile {
public static void main(String
[] args
) throws IOException
{
File f
=new File("D:/a.txt");
System
.out
.println(f
);
f
.renameTo(new File("d:/bb.txt"));
System
.out
.println(System
.getProperty("user.dir"));
File f2
=new File("cc.txt");
System
.out
.println("File是否存在:"+f2
.exists());
System
.out
.println("File是否是目录:"+f2
.isDirectory());
System
.out
.println("File是否是文件:"+f2
.isFile());
System
.out
.println("File最后修改时间:"+new Date(f2
.lastModified()));
System
.out
.println("File的大小:"+f2
.length());
System
.out
.println("File的文件名:"+f2
.getName());
System
.out
.println("File的目录路径:"+f2
.getPath());
System
.out
.println("File的目录路径:"+f2
.getAbsolutePath());
File f3
=new File("d:/电影/华语/大陆");
boolean flag
=f3
.mkdirs();
System
.out
.println(flag
);
File file
=new File("d:/z/z.txt");
boolean flag1
=file
.exists();
if(flag
){
boolean flagd
=file
.delete();
if(flagd
){
System
.out
.println("删除成功");
}else{
System
.out
.println("删除失败");
}
}else{
boolean flagn
=true;
try{
File dir
=file
.getParentFile();
dir
.mkdirs();
flagn
=file
.createNewFile();
System
.out
.println("创建成功");
}catch(IOException e
){
System
.out
.println("创建失败");
e
.printStackTrace();
}
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-2745.html