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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
