// 文件大小
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);
}
}
}
}
转载于:https://blog.51cto.com/wuyongzhiyi/1960808