java 文 件 类(File)
File类:创建和删除文件,不能操作文件内容
File(文件):文件夹/文件(.txt、.ext、压缩包、安装包)
File file = new File(地址);
file.length():文件大小(B、KB、MB、GB、TB、PB)
exists判断文件是否存在
getPath():获取文件的相对路劲
getAbsolutePath():获取文件的绝对路劲
lastModified():获取最后一次修改文件的毫秒值
createNewFile():创建文件
mkdir():创建单层目录
mkdirs():创建多层目录
isFile():判断给定的文件是否是文件
isDirectory():判断给定的文件是否是目录
delete():删除文件,慎用(会直接删除,不能撤回)
getName():获取文件名称
listFiles():获取指定目录下所有子文件
没有参数就是获取所有文件
带参数就是过滤,找到相应文件
canExecute():判断文件是否能打开
canRead():判断文件是否可读
canWrite():判断文件是否可写
setReadOnly():设置文件为只读
setLastModified(10000L):设置文件内容的最后修改时间 isHidden():判断文件是否为隐藏文件
public static void main(String[] args) {
File file = new File("./src/filetest/Demo01.java");
long l = file.length();
System.out.println(l);
boolean exists = file.exists();
System.out.println(exists);
String path = file.getPath();
System.out.println(path);
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
long lastModified = file.lastModified();
System.out.println(lastModified);
Date date = new Date(lastModified);
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = s.format(date);
System.out.println(format);
try {
boolean createNewFile = file.createNewFile();
System.out.println(createNewFile);
} catch (IOException e) {
e.printStackTrace();
}
File file2 = new File("D:/Desktop/test/a.txt");