package com.msb.files;
import java.io.File;
import java.io.IOException;
/**
* @author : liu
* 日期:14:45:38
* 描述:IntelliJ IDEA
* 版本:1.0
*/
public class Test01 {
//这是一个main方法:是程序的入口
public static void main(String[] args) throws IOException {
//将文件封装为一个File对象
File f=new File("d:\\test.txt");
File f2=new File("d:/test.txt");
//File.separator用来获取当前操作系统的路径拼接符号
File f3=new File("d:"+File.separator+"test.txt");
//常用方法
System.out.println("文件是否可读:"+f.canRead());
System.out.println("文件是否可写:"+f.canWrite());
System.out.println("文件的名字:"+f.getName());
System.out.println("上级目录:"+f.getParent());
System.out.println("是否是一个目录:"+f.isDirectory());
System.out.println("是否是一个文件:"+f.isFile());
System.out.println("是否隐藏:"+f.isHidden());
System.out.println("文件的大小:"+f.length());
System.out.println("是否存在"+f.exists());
/*if (f.exists()){//如果文件存在,将文件删除
f.delete();
}else {//如果不存在,就创建这个文件
f.createNewFile();
}*/
System.out.println(f==f2);//比较两个对象的地址
System.out.println(f.equals(f2));//比较两个对象对应的文件路径
//根据经相关的:
System.out.println("绝对路径:"+f.getAbsolutePath());
System.out.println("相对路径:"+f.getPath());
System.out.println("toString:"+f.toString());
System.out.println("=======================");
File f5=new File("demo.txt");
if (!f5.exists()){
f.createNewFile();
}
//绝对路径值得就是真实的精准的路径
System.out.println("绝对路径:"+f5.getAbsolutePath());
//相对路径:有一个参照物,相对这个参照物的路径。
System.out.println("相对路径:"+f5.getPath());
//toString的效果永远是相对路径
System.out.println("toString:"+f5.toString());
}
}