java.io.File中一些函数的学习

来源:互联网 发布:强行脱女朋友裤子知乎 编辑:程序博客网 时间:2024/06/06 23:51

Example

public static long diskUsage(File root){    long total = root.length();    if(root.isDirectory()){        for(String childname: root.list()){            File child = new File(root, childname);            total += diskUsage(child);        }    }    return total;}public static void main(String[] args){    System.out.println(diskUsage(new File("/Users/zhongwen/Desktop/ForDreamForFuture/mycode")));}

用到的Methods

  • new File(pathString) or new File(parentFile, childString)
    A new File instance can be constructed either by providing the full path as a string,
    or by providing an existing File instance that represents a directory and a string that designates the name of a child entry within that directory.
    also,
    File(String parent, String child)
    Creates a new File instance from a parent pathname string and a child pathname string.

  • file.length()
    Returns the length of the file denoted by this abstract pathname.

  • file.isDirectory()
    Tests whether the file denoted by this abstract pathname is a directory.

  • file.list()
    Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

ps:在terminal中查看某个文件夹的disk usage的命令是 du -a filepath

0 0
原创粉丝点击