How can I read the file information (for example size, line count, last modification, etc) from a file in the file-system or the directory content with Java? I presently working on linux operating system. Kindly give some ideas particularly for Linux OS.
Thanks in Advance.
For what it is worth, "line count" is not a file attribute in UNIX or Linux.
If you want a line count (assuming that it is a valid concept) you need to figure it out by reading the file. Simply counting newline characters gives a roughly correct answer for "text" files, but it may give a bogus answer in other cases. A complete answer involves:
The other attributes you listed (file size and modification time) can be accessed in Java using the classic java.io.File
API, or the new Java 7 java.nio.file.Files
API.
Try looking in to the File api . There are a bunch of methods like 'lastModified', 'length', and most of what you should need.
Use File class (except for line count - see this Q/A for that )
java.io.File.length()
Also, if you can get your Java program to make a system call, you can use Unix's wc -l
command to count the lines for you - might be faster than Java but costs you a spawned-off process
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.