Java 獲取文件大小

// 文件大小
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);
            }
        } 
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章