资讯专栏INFORMATION COLUMN

05File类之File类的获取功能的方法

honhon / 1863人阅读

摘要:获取的就是构造方法传递路径的结尾部分文件文件夹将此转换为路径名字符串。获取的构造方法中传递的路径无论路径是绝对的还是相对的方法返回的都是绝对路径

package com.itheima.demo01.File;

import java.io.File;

/*

File类获取功能的方法
    - public String getAbsolutePath() :返回此File的绝对路径名字符串。
    - public String getPath() :将此File转换为路径名字符串。
    - public String getName()  :返回由此File表示的文件或目录的名称。
    - public long length()  :返回由此File表示的文件的长度。

*/
public class Demo03File {

public static void main(String[] args) {
    show04();
}

/*
    public long length()  :返回由此File表示的文件的长度。
    获取的是构造方法指定的文件的大小,以字节为单位
    注意:
        文件夹是没有大小概念的,不能获取文件夹的大小
        如果构造方法中给出的路径不存在,那么length方法返回0
 */
private static void show04() {
    File f1 = new File("C:developa1.jpg");
    long l1 = f1.length();
    System.out.println(l1);//780831字节

    File f2 = new File("C:developa2.jpg");
    System.out.println(f2.length());//0

    File f3 = new File("C:developa");
    System.out.println(f3.length());//0 文件夹没有大小概念的
}

/*
    public String getName()  :返回由此File表示的文件或目录的名称。
    获取的就是构造方法传递路径的结尾部分(文件/文件夹)
 */
private static void show03() {
    File f1 = new File("C:UsersitcastIdeaProjectsshungyuana.txt");
    String name1 = f1.getName();
    System.out.println(name1);//a.txt

    File f2 = new File("C:UsersitcastIdeaProjectsshungyuan");
    String name2 = f2.getName();
    System.out.println(name2);//shungyuan
}

/*
    public String getPath() :将此File转换为路径名字符串。
    获取的构造方法中传递的路径

    toString方法调用的就是getPath方法
    源码:
        public String toString() {
            return getPath();
        }
 */
private static void show02() {
    File f1 = new File("C:UsersitcastIdeaProjectsshungyuana.txt");
    File f2 = new File("a.txt");
    String path1 = f1.getPath();
    System.out.println(path1);//C:UsersitcastIdeaProjectsshungyuana.txt
    String path2 = f2.getPath();
    System.out.println(path2);//a.txt

    System.out.println(f1);//C:UsersitcastIdeaProjectsshungyuana.txt
    System.out.println(f1.toString());//C:UsersitcastIdeaProjectsshungyuana.txt
}

/*
    public String getAbsolutePath() :返回此File的绝对路径名字符串。
    获取的构造方法中传递的路径
    无论路径是绝对的还是相对的,getAbsolutePath方法返回的都是绝对路径
 */
private static void show01() {
    File f1 = new File("C:UsersitcastIdeaProjectsshungyuana.txt");
    String absolutePath1 = f1.getAbsolutePath();
    System.out.println(absolutePath1);//C:UsersitcastIdeaProjectsshungyuana.txt

    File f2 = new File("a.txt");
    String absolutePath2 = f2.getAbsolutePath();
    System.out.println(absolutePath2);//C:UsersitcastIdeaProjectsshungyuana.txt
}

}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76017.html

相关文章

  • 07File类之File删除功能,创建文件夹,文件方法

    摘要:类创建删除功能的方法当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。删除由此表示的文件或目录。 package com.itheima.demo01.File; import java.io.File;import java.io.IOException; /* File类创建删除功能的方法 - public boolean createNewFile() :当且仅当具...

    pingan8787 评论0 收藏0
  • 06File类之File判断功能方法

    摘要:类判断功能的方法此表示的文件或目录是否实际存在。用于判断构造方法中的路径是否存在存在不存在相对路径 package com.itheima.demo01.File; import java.io.File; /* File类判断功能的方法 - public boolean exists() :此File表示的文件或目录是否实际存在。 - public boolean i...

    willin 评论0 收藏0
  • 08File类之File遍历文件夹目录方法

    摘要:类遍历文件夹目录功能返回一个数组,表示该目录中的所有子文件或目录。遍历构造方法中给出的目录会获取目录中所有文件文件夹的名称把获取到的多个名称存储到一个类型的数组中这个遍历出来的显示文件夹或者文件的路径 package com.itheima.demo01.File; import java.io.File; /* File类遍历(文件夹)目录功能 - public String...

    edgardeng 评论0 收藏0
  • 02File类之File静态成员变量

    摘要:与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。操作路径路径不能写死了路径分隔符分号冒号文件名称分隔符反斜杠正斜杠 showImg(https://segmentfault.com/img/bVbwc7z); showImg(https://segmentfault.com/img/bVbwc8i); showImg(https://segmentfault.com/img...

    venmos 评论0 收藏0
  • 04File类之File构造方法

    摘要:参数把路径分成了两部分父路径子路径好处父路径和子路径可以单独书写使用起来非常灵活父路径和子路径都可以变化父路径是类型可以使用的方法对路径进行一些操作再使用路径创建对象根据路径名字符串和路径名字符串创建一个新实例。 showImg(https://segmentfault.com/img/bVbwdvj?w=1344&h=684);package com.itheima.demo01.F...

    paulli3 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<