Possible Duplicate:
java get file size efficiently可能重复:java有效地获取文件大小
I have a File called filename which is located in E://file.txt
.
我有一个名为filename的文件,它位于E://file.txt中。
FileInputStream fileinputstream = new FileInputStream(filename);
What I want to do is to calculate the size of this file in bytes. How can I have this done?
我想要做的是以字节为单位计算此文件的大小。我怎么能这样做?
4 个解决方案
#1
171
You can use the length()
method on File
which returns the size in bytes.
您可以在File上使用length()方法,该方法返回以字节为单位的大小。
#2
36
You don't need FileInputStream to calculate file size, new File(path_to_file).length()
is enough. Or, if you insist, use fileinputstream.getChannel().size()
.
您不需要FileInputStream来计算文件大小,新文件(path_to_file).length()就足够了。或者,如果你坚持,使用fileinputstream.getChannel()。size()。
#3
5
You can do that simple with Files.size(new File(filename).toPath())
.
你可以使用Files.size(new File(filename).toPath())来做到这一点。
#4
3
public static void main(String[] args) {
try {
File file = new File("test.txt");
System.out.println(file.length());
} catch (Exception e) {
}
}
#1
171
You can use the length()
method on File
which returns the size in bytes.
您可以在File上使用length()方法,该方法返回以字节为单位的大小。
#2
36
You don't need FileInputStream to calculate file size, new File(path_to_file).length()
is enough. Or, if you insist, use fileinputstream.getChannel().size()
.
您不需要FileInputStream来计算文件大小,新文件(path_to_file).length()就足够了。或者,如果你坚持,使用fileinputstream.getChannel()。size()。
#3
5
You can do that simple with Files.size(new File(filename).toPath())
.
你可以使用Files.size(new File(filename).toPath())来做到这一点。
#4
3
public static void main(String[] args) {
try {
File file = new File("test.txt");
System.out.println(file.length());
} catch (Exception e) {
}
}