Home > Java > JavaBase > Java file operation-get file or directory information

Java file operation-get file or directory information

王林
Release: 2019-11-30 13:39:46
forward
2790 people have browsed it

Java file operation-get file or directory information

The example is as follows:

public class TestDemo3 {
    public static void main(String[] args) {
        File file=new File("h:"+File.separator+"my.exe");
        if (file.exists()){
            System.out.println("是否为文件:"+file.isFile());
            System.out.println("是否为目录:"+file.isDirectory());
            System.out.println("文件大小:"+new BigDecimal((double)file.length()/1024/1024)
            .divide(new BigDecimal(1),2,BigDecimal.ROUND_HALF_UP)+"M");
            System.out.println("上次修改时间:"+new SimpleDateFormat("YYYY-MMM-dd hh:MM:ss")
            .format(new Date(file.lastModified())));
            System.out.println("上次修改时间:"+file.lastModified());
        }
    }
}
Copy after login

Online learning video tutorial sharing: java online learning

The running results are as follows:

Java file operation-get file or directory information

File size:

Because the file.length() method returns the value long is not accurate enough, so it is converted to double and converted to a format that retains two decimal places using the BigDecimal method.

BigDecimal (1) , 2, Bigdecimal.ROUND_HALF_UP, respectively represent the dividend, the number of reserved digits, and the carry mode. The only mode of this program is up carry.

Last modification time:

lastModified()The return value type of the method is long, first convert it to date Type, use SimpleDateFormate to convert it to a common format.

For more related articles and tutorials, please visit: Introduction to java programming

The above is the detailed content of Java file operation-get file or directory information. For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template