package IOdemo;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
public class FileMethodDemo {
public static void main(String[] args) throws IOException {
File file = new File("c" + File.separator + "abc" + File.separator + "1.txt");
method1(file);
method2(file);
}
private static void method2(File file) throws IOException {
//判断是否可以创建文件
boolean is_create = file.createNewFile();
//判断是否删除了文件
boolean is_del = file.delete();
//创建文件夹
boolean cre_dir = file.mkdir();
//创建多级文件夹
boolean cre_dirs = file.mkdirs();
//删除文件夹(文件夹为空才被删除)
boolean del_dir = file.delete();
}
private static void method1(File file) {
// 获取文件名字
String name = file.getName();
// 文件大小
long len = file.length();
// 文件所在的文件夹
String dir = file.getParent();
// 绝对路径
String Apath = file.getAbsolutePath();
// file对象中的封装路径
String path = file.getPath();
// 最后一次修改时间
long time = file.lastModified();
String date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(time);
System.out.println(date);
// 文件是否隐藏
boolean is_hidden = file.isHidden();
// 文件是否存在
boolean is_exist = file.exists();
//是不是文件
boolean is_file = file.isFile();
//是不是文件夹
boolean is_dir = file.isDirectory();
//获取指定的分区中未分配的字节数
long free_space = file.getFreeSpace();
//获取指定的分区中总共的字节数
long total_space = file.getTotalSpace();
//获取指定的分区中使用的字节数
long used_space = file.getUsableSpace();
//获取指定文件夹中所有的文件和目录名称
String[] list_file_dir = file.list();
//获取指定文件夹中的文件对象(可以获取里面文件的名字,大小等)
File[] list_file = file.listFiles();
}
}
java中对文件的常见操作方法
最新推荐文章于 2021-11-23 16:46:09 发布