java获取文件大小

 时间:2026-02-14 17:15:13

1、使用File的length()方法:

public static void main(String[] args) { 

File f= new File("D:\\test.zip"); 

if (f.exists() && f.isFile()){ 

 logger.info(f.length());

 }else{ 

 logger.info("file doesn't exist or is not a file"); }}

java获取文件大小

2、 通过FileInputStream来获取的文件大小(当文件太大会出现问题):

public static void main(String[] args) {

 FileInputStream fis= null; 

try{  File f= new File("D:\\test.zip");  

fis= new FileInputStream(f);  

logger.info(fis.available());

 }catch(Exception e){ 

 logger.error(e);

 } finally{  if (null!=fis){  

 try {    fis.close(); 

  } catch (IOException e) {  

  logger.error(e);   }  } }}

java获取文件大小

3、通过FileInputStream来获取的文件大小(解决了当文件太大出现问题)

public static void main(String[] args) {

 FileChannel fc= null; 

try {  File f= new File("D:\\test.zip");

 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获取文件大小

  • Liberty(WLP)配置单项SSL(https)方式访问
  • notepad++设置最近文件记录仅文件名
  • 【linux】下jdk安装以及配置【tar.gz版】
  • 云电脑中windows server 2012 多用户配置说明
  • Oracle 故障:[1]rac 不能启动
  • 热门搜索
    收条怎么写范本 崖柏怎么盘 千奇百怪的近义词 lol怎么观战 感冒头痛 儿童诗怎么写 佳能打印机怎么样 京东金融怎么样 煎熬的近义词 三个月宝宝拉肚子怎么办