|
Java 实例 - 获取文件大小
以下实例演示了使用 File 类的 file.exists() 和 file.length() 方法来获取文件大小,以字节计算(1KB=1024字节 ): Main.java 文件 import java . io . File ;
public class Main {
public static long getFileSize ( String filename ) {
File file = new File ( filename ) ;
if ( ! file . exists ( ) || ! file . isFile ( ) ) {
System . out . println ( " 文件不存在 " ) ;
return - 1 ;
}
return file . length ( ) ;
}
public static void main ( String [ ] args ) {
long size = getFileSize ( " c:/java.txt " ) ;
System . out . println ( " java.txt文件大小为: " + size ) ;
}
} |
以上代码运行输出结果为(java.txt 文件位于 C 盘):
java.txt文件大小为: 480
程序猿的技术大观园:www.javathinker.net
[这个贴子最后由 flybird 在 2020-01-26 17:42:57 重新编辑]
|
网站系统异常
系统异常信息 |
Request URL:
http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=1503&pages=31
java.lang.NullPointerException
如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员。
|
|