Java File length() Method
File.length() is a method in Java's java.io.File a commonly used method of the class, used to obtain the size of a file (in bytes). This method returns a long value representing the byte length of the specified file.
Method Syntax
public
long
length
(
)
Return Value
Returns the size of the file (in bytes). If the file does not exist or is a directory, returns 0L.
Use Cases
length() The method is especially useful in the following situations:
- Check whether the file size meets requirements
- Monitor changes in file size
- Calculate disk space usage
- Display progress during file upload/download
Basic Usage Example
Example 1: Get file size
Example
import
java.io.File
;
public
class
FileLengthExample
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"example.txt"
)
;
if
(
file.
exists
(
)
)
{
long
fileSize
=
file.
length
(
)
;
System
.
out
.
println
(
"File size: "
fileSize
" bytes"
)
;
}
else
{
System
.
out
.
println
(
"File does not exist"
)
;
}
}
}
Code explanation
- creation
Fileobject points to the target file - Check whether the file exists
- If it exists, call
length()method to get the file size - Print the file size information
Notes
1. Case where the file does not exist
If the file does not exist,length() the method returns 0 instead of throwing an exception. Therefore, it is best to check whether the file exists before calling:
Example
if
(
file.
exists
(
)
)
{
long
size
=
file.
length
(
)
;
// Process file size
}
else
{
// Logic for handling non-existent file
}
2. Case of a directory
For a directory,length() the method also returns 0. If you need to get the directory size, you need to recursively calculate the sizes of all files in the directory.
3. Symbolic links
If the file is a symbolic link,length() returns the size of the actual file the link points to.
4. Large file handling
Because length() returns a long value, which can handle files up to 2^63-1 bytes (about 8 EB), sufficient for the vast majority of scenarios.
Advanced usage
Example 2: Format file size
Example
import
java.io.File
;
public
class
FileSizeFormatter
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"largefile.zip"
)
;
long
size
=
file.
length
(
)
;
System
.
out
.
println
(
"Raw size: "
size
" bytes"
)
;
System
.
out
.
println
(
"Formatted size: "
formatFileSize
(
size
)
)
;
}
public
static
String
formatFileSize
(
long
size
)
{
if
(
size
<
1024
)
{
return
size" B"
;
}
int
exp
=
(
int
)
(
Math
.
log
(
size
)
/
Math
.
log
(
1024
)
)
;
char
unit
=
"KMGTPE"
.
charAt
(
exp
1
)
;
return
String
.
format
(
"%.1f %sB"
, size
/
Math
.
pow
(
1024
, exp
)
, unit
)
;
}
}
Output example
原始大小: 1500000 字节
格式化大小: 1.4 MB
Performance considerations
File.length() Calling the method is lightweight because it only queries the file system's metadata and does not actually read the file content. However, you should be careful in the following situations:
- Network file system: accessing the metadata of a remote file system may have latency
- Frequent calls: if you need to get the file size frequently, consider caching the result
- Real-time monitoring: for scenarios that monitor file size changes in real time, you may need to combine with
WatchServiceUse
Frequently Asked Questions
Q1: Why does calling length() on a directory return 0?
A: In most file systems, a directory itself does not occupy storage space (or occupies a small fixed amount),length() so the method is designed to return 0 for directories.
Q2: Does the length() method block?
A: Generally it does not, unless you are accessing a network file system with poor network conditions.
Q3: How to get the total size of a directory?
A: You need to recursively traverse all files in the directory and sum up their length() values.
Summary
File.length() It is a simple but practical method for obtaining file size information. When using it, you need to pay attention to edge cases such as whether the file exists and whether it is a directory. Combined with other file operation methods, you can build powerful file-handling features.
Java File