java语言的IO的File类的讲解

来源:互联网 发布:xp系统mac地址怎么查 编辑:程序博客网 时间:2024/05/14 11:42

package cn.itcast.files;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileTest {
 public static void main(String[] args) {
  File file = new File("a.txt");
  if(file.exists())
  {
        file.delete();
  }else
  {
   try {
        file.createNewFile();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  //这是得到的刚才建立的文件夹的名字
  System.out.println(file.getPath());
  //这个返回的是一个绝对路径
  System.out.println(file.getAbsolutePath());
  //返回该文件父路径
  System.out.println(file.getParent());
  //访问该文件是否是目录
  System.out.println("isDirrectory:" + file.isDirectory());
  //返回该文件是否存在
  System.out.println("isExists:" + file.exists());
  //返回该文件最后一次修改的时间
  Date date = new Date(file.lastModified());
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  System.out.println(dateFormat.format(date));
 }

}