//FileTest.java import java.io.*; import java.util.*; public class FileTest { public static void main(String[] args) { // TODO: Add your code here File f = new File("1.txt"); //创建一个文件对象,并传入一个文件名作为构造方法参数 if(f.exists()){ //f.exists()判断文件是否存在 f.delete(); //删除文件 } else{ try{ f.createNewFile();//File.createNewFile()可能会发生异常。比如硬盘空间不足,不可写等 }catch(Exception e){ e.printStackTrace(); } } System.out.println("FileName: "+f.getName()); //File.getName()获取文件对象的文件名 System.out.println("FilePath: "+f.getPath()); //获取文件路径 System.out.println("FileAbsPath: "+f.getAbsolutePath()); //获取文件绝对路径 System.out.println("FileParent: "+f.getParent()); //获取文件父目录 System.out.println(f.exists()?"Exists":"Not Existes"); // System.out.println(f.canRead()?"Read":"not Read"); //判断文件是否可读 System.out.println(f.isDirectory()?"Directory":"not Directory"); //判断是否为目录 System.out.println("FileLastModified: "+new Date(f.lastModified())); //File.lastModified()获取最后修改时间(long time)。new Date(long time)通过Date类的构造函数将long型时间数据转换成格式时间 } }