IO流之文件的操作
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileOperation {
public static void main(String[] args) {
File file1 = new File("D:/word.txt");
System.out.println("判断文件是否存在:" + file1.exists());
File file2 = new File("D:/", "word.txt");
File dir = new File("D:/");
File file3 = new File(dir, "word.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file2.getAbsolutePath());
System.out.println(file3.getAbsolutePath());
System.out.println("文件名:" + file1.getName());
System.out.println("绝对路径:" + file1.getAbsolutePath());
System.out.println("是否是隐藏文件:" + file1.isHidden());
System.out.println("文件的字节数:" + file1.length());
Date date = new Date(file1.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("文件的最后修改时间:" + sdf.format(date));
System.out.println("删除文件是否成功:" + file1.delete());
try {
System.out.println("创建文件是否成功:" + file1.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}