Java如何创建临时文件并打印File各种属性

414 阅读1分钟

临时文件使用完毕需要自己清理,避免沾满磁盘空间。

    public static void main(String[] args) throws IOException {
        File temp = File.createTempFile("test", ".archive");

        System.out.println(temp.getName()); // test3055590609638838031.archive
        System.out.println(temp.getPath()); // /var/folders/2x/65r35n150q118b_chsszlh1c0000gn/T/test6559503323010508065.archive

        System.out.println(temp.getUsableSpace()); // 34223104000
        System.out.println(temp.getFreeSpace()); // 44578095104
        System.out.println(temp.getTotalSpace()); // 500068036608

        System.out.println(temp.getAbsolutePath()); // /var/folders/2x/65r35n150q118b_chsszlh1c0000gn/T/test6559503323010508065.archive
//        System.out.println(temp.getAbsoluteFile());

        System.out.println(temp.getCanonicalPath()); // /private/var/folders/2x/65r35n150q118b_chsszlh1c0000gn/T/test6559503323010508065.archive
//        System.out.println(temp.getCanonicalFile());

        System.out.println(temp.getParent()); // /var/folders/2x/65r35n150q118b_chsszlh1c0000gn/T
//        System.out.println(temp.getParentFile());

        System.out.println(temp.delete()); // true
        
        System.out.println(File.pathSeparator); // :
        
        System.out.println(File.separator); // /
    }