95、file常用方法:获取类

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);
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值