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

Java file operation-get file or directory information

王林
王林forward
2019-11-30 13:39:462676browse

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

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete