您的位置:首页 > 编程语言 > Java开发

Java 获取文件大小

2017-08-29 14:29 393 查看
// 文件大小
public static void main(String[] args) {
File f = new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso");
if (f.exists() && f.isFile()) {
logger.info(f.length());
} else {
logger.info("file doesn't exist or is not a file");
}
}

/*
* 针对流式方法读取大文件大小也不是不可行, 只是不能再使用传统的java.io.*下的包了,
* 这里要用到java.nio.*下的新工具——FileChannel
*/
public static void main(String[] args) {
FileChannel fc= null;
try {
File f= new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso");
if (f.exists() && f.isFile()){
FileInputStream fis= new FileInputStream(f);
fc= fis.getChannel();
logger.info(fc.size());
}else{
logger.info("file doesn't exist or is not a file");
}
} catch (FileNotFoundException e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
} finally {
if (null!=fc)){
try{
fc.close();
}catch(IOException e){
logger.error(e);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 文件大小