import java.io.File;
import java.io.IOException;
import java.util.Date;
public class FileDemo {
/*
* java.io包
* File:文件或目录(文件夹)的路径名的抽象表示形式。
* File实例代表抽象路径表示文件或目录(文件夹)。
* File的构造方法:
* 1.new File(String pathName):
* 该实例代表参数String路径的文件或目录
* 2.new File(String parent,String child):
* 该实例代表String 参数1父级路径下的子文件或目录(文件夹)。
* 3.new File(File parent, String child):
* 该实例代表File类型参数1父级路径文件夹下的子文件或目录(文件夹)。
*/
public static void main(String[] args) {
File file = new File("D:/demo/a.txt");
File file2 = new File("D:/demo", "b.txt");
File parent = new File("D:/demo/temp/aa");
File file3 = new File(parent, "c.txt");
/*
* boolean | createNewFile():创建文件。
* 父级路径必须存在,否则会抛异常。
* 返回是否创建成功
*/
try {
System.out.println(file.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
/*File file4 = new File("D:/demo/aa/bb/cc/dd/d.txt");
try {
file4.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
*/
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
/*
* 创建文件夹
* mkdirs():创建多级目录
*/
parent.mkdirs();
try {
file3.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
File file5 = new File("a.txt");
try {
file5.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//boolean | canExecute():能否可执行
//Windows中不好使,Linux好使
boolean bool = file.canExecute();
System.out.println(bool);//true
/*
* boolean | canRead():能否可读
* boolean | canWrite():能否可写
*/
bool = file.canRead();
System.out.println(bool);
bool = file.canWrite();
System.out.println(bool);
/*
* compareTo(File pathname):比较的是路径 字符串
*/
File f = new File("G:/a/b.txt");
int i = file.compareTo(f);
System.out.println(i);
/*
* deleteOnExit():删除
* Java的File类中有两个delete方法:delete和deleteOnExit
*delete无需解释,为直接删除,
* deleteOnExit文档解释为:在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。
* 也就是说,程序运行deleteOnExit成功后,File并没有直接删除,而是在虚拟机正常运行结束后才会删除
*/
// file.deleteOnExit();
/*
* boolean | exists():
* 判断该文件或目录在硬盘中是否存在
*/
bool = file.exists();
System.out.println(bool);
bool = f.exists();
System.out.println(bool);
/*
* String | getAbsolutePath():
* 获得该文件或目录的绝对路径字符串表示形式。
* File | getAbsoluteFile():
* 获得该文件或目录的绝对路径File类型对象。
*/
String str = file5.getAbsolutePath();
System.out.println(str);//D:\demo\workspace\ioTest\a.txt
File f5 = file5.getAbsoluteFile();
System.out.println(f5);//D:\demo\workspace\ioTest\a.txt
/*
* long | getFreeSpace():获得该盘符的剩余空间
* long | getTotalSpace():获得该盘符的总空间
*/
long freeSpace = file.getFreeSpace();
System.out.println(freeSpace);//259748003840
long l = file.getTotalSpace();
System.out.println(l);//272379379712
/*
* String | getName():获得文件或目录的名字
*/
String name = file.getName();
System.out.println(name);//a.txt
/*
* String | getParent():返回该文件的父级路径String类型
* File | getParentFile():返回该文件的父级路径的File对象。
*/
String s = file.getParent();
System.out.println(s);//D:\demo
File fileP = file.getParentFile();
System.out.println(fileP);//D:\demo
//创建文件:
//1.创建File对象
File file4 = new File("D:/demo/aa/bb/cc/dd/d.txt");
//2.获得该文件的父级路径File对象
File file4P = file4.getParentFile();
//3.如果该父级路径表示的目录不存在,则创建多级目录
if(!file4P.exists()){
file4P.mkdirs();
}
//4.如果该文件不存在,则创建该文件
if(!file4.exists()){
try {
file4.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("------------------------------");
/*
* String | getPath()
* 获得路径。file对象封装什么路径,就返回什么路径。
*/
System.out.println(file5.getPath());
/*
* boolean | isAbsolute()
* 是否是绝对的
*/
bool = file5.isAbsolute();
System.out.println(bool);//false
/*
* boolean | isDirectory():是否是目录(文件夹)
*/
bool = parent.isDirectory();
System.out.println(bool);//true
/*
* boolean | isFile():是否是文件
*/
bool = file.isFile();
System.out.println(bool);
/*
* boolean | isHidden():是否是隐藏文件
*/
bool = file.isHidden();
System.out.println(bool);
/*
* long | lastModified():
* 最后修改时间
*/
l = file.lastModified();
System.out.println(l);
Date d = new Date(l);
System.out.println(d);
/*
* long | length():该文件的字节数
*/
l = file.length();
System.out.println(l);
/*
* String[] | list():返回当前目录下的所有文件或目录的String类型表示形式
*/
File file6 = new File("D:/demo");
String[] strs = file6.list();
for (String ss : strs) {
System.out.println(ss);
}
/*
* File[] | listFiles():
* 获得当前目录下所有的文件和目录的File对象。
*/
File[] files = file6.listFiles();
for (File ff : files) {
System.out.println(ff);
}
}
}