public class Hello {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("File类只用于表示文件或者目录的信息,不能对文件或目录进行访问");
System.out.println("---------目录属性--------------");
File mulu = new File("G:/ceshifile/hello");
System.out.println(mulu.length());
System.out.println(mulu.lastModified());
System.out.println(mulu.getName());
System.out.println(mulu.getPath());
System.out.println(mulu.exists());
System.out.println(mulu.isFile());
System.out.println(mulu.isDirectory());
System.out.println(mulu.canRead());
System.out.println(mulu.canWrite());
System.out.println("--------文件属性---------------");
File wenjian = new File("G:/ceshifile/hello.txt");
System.out.println(wenjian.length());
System.out.println(wenjian.lastModified());
System.out.println(wenjian.getName());
System.out.println(wenjian.getPath());
System.out.println(wenjian.exists());
System.out.println(wenjian.isFile());
System.out.println(wenjian.isDirectory());
System.out.println(wenjian.canRead());
System.out.println(wenjian.canWrite());
System.out.println("----------创建目录-------------");
File xinjianmulu = new File("demo");
if (!xinjianmulu.exists()) {
xinjianmulu.mkdir();// 新建文件夹
}
System.out.println(xinjianmulu.exists());
System.out.println(xinjianmulu.getPath());
System.out.println(xinjianmulu.getAbsolutePath());
System.out.println(xinjianmulu.getCanonicalPath());
System.out.println("---------创建文件--------------");
File xinjianwenjian = new File(xinjianmulu, "hello.txt");
if (!xinjianwenjian.exists()) {
xinjianwenjian.createNewFile();// 新建文件
}
System.out.println(xinjianwenjian.getPath());
System.out.println(xinjianwenjian.getCanonicalPath());
System.out.println("----------------------------------");
File mulu2 = new File(xinjianmulu,"demo2");
if (!mulu2.exists()) {
mulu2.mkdir();// 新建文件夹
}
File wenjian2 = new File(mulu2, "hello2.txt");
if (!wenjian2.exists()) {
wenjian2.createNewFile();// 新建文件
}
System.out.println("---------创建文件,连带创建目录--------------");
File mulu3 = new File(new File(xinjianmulu,"demo3"),"demo3s");
if (!mulu3.exists()) {
mulu3.mkdirs();// 新建文件夹包同时新建父文件夹
}
System.out.println("-----------------------");
}
}
阅读(1073) | 评论(0) | 转发(0) |