File关于获取的相关方法
public String getAbsolutePath()返回此抽象路径名的绝对路径名字符串。
不管调用该方法的对象在构造时是相对路径还是绝对路径,返回的都是绝对路径,
不会去判断是否存在该文件或文件夹
public String getPath()将此抽象路径名转换为一个路径名字符串。
调用的对象创建对象构造函数传入的是什么路径,返回的就是什么路径
public String toString()返回此抽象路径名的路径名字符串。
其实底层调用的是getPath();
public String getName()返回由此抽象路径名表示的文件或目录的名称。
public long length()返回由此抽象路径名表示的文件的长度。
如果是真实存在的文件,会返回它的长度,如果是不存在的或者是文件夹,返回的是0
import java.io.File;
public class Demo03GetMethod {
public static void main(String[] args) {
showAbsolutePath();
System.out.println("===========================");
showPath();
System.out.println("===========================");
showName();
System.out.println("===========================");
showLength();
}
/*绝对路径*/
public static void showAbsolutePath(){
File file01 = new File("E:\\Music\\歌曲\\刘欢-在路上.mp3");
System.out.println(file01.getAbsolutePath());
File file02 = new File("刘欢-在路上.mp3");
System.out.println(file02.getAbsolutePath());
}
/*相对路径*/
public static void showPath(){
File file01 = new File("E:\\Music\\歌曲\\刘欢-在路上.mp3");
System.out.println(file01.getPath());
File file02 = new File("刘欢-在路上.mp3");
System.out.println(file02.getPath());
System.out.println(file02.toString());
}
/*获取路径名*/
public static void showName(){
File file01 = new File("E:\\Music\\歌曲\\刘欢-在路上.mp3");
System.out.println(file01.getName());
File file02 = new File("刘欢-在路上.mp3");
System.out.println(file02.getName());
}
/*获取文件大小*/
public static void showLength(){
File file01 = new File("E:\\Music\\歌曲\\刘欢-在路上.mp3");
System.out.println(file01.length());
File file02 = new File("E:\\Music\\歌曲\\刘欢-在路上.mp3");
System.out.println(file02);
File file03 = new File("E:\\Music\\歌曲\\我是假的.mp3");
System.out.println(file03);
}
}