Java 读取某文件下的所有文件名称以及大小,并输出在xls表格里

2023-10-31

方法一: 

package com.test.demo.test;

import lombok.Data;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: GetFileSize.java
 * @Project: byy
 * @Description: 获取文件大小
 * @Date: 2019-07-08 10:11
 * @Version: V2.0
 **/
public class GetFileSize {

    public static void main(String[] args) {

        List<Bean> data = new ArrayList<Bean>();
        //输入一个路径
        String path="G:\\temp";
        getFile(data, path);
        save(data);
        System.out.println("统计完毕");
    }


    private static void save(List<Bean> data) {
        OutputStream os = null;
        OutputStreamWriter osw = null;
        try {
            //将统计的数据大小统计为文件     路径
            os = new FileOutputStream(new File("C:\\Users\\admin\\Desktop\\d1.csv"));
            osw = new OutputStreamWriter(os, "GBK");
            for (Bean bean : data) {
                osw.write(bean + "\r\n");
            }
            osw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void getFile(List<Bean> data, String path) {

        File f = new File(path);
        File[] fs = f.listFiles();

        if (fs == null) {
            return;
        }
        for (File file : fs) {
            //将统计的文件的字节数(单位:B)    方便计算大小
            if (file.isFile()) {
                //getFileSize(file)返回的int最大显示2G
                //System.out.println("文件名:"+ file.getName()+",文件大小是:"+ getFileSize(file));
                //file.length()可显示完整大小
                System.out.println("文件名:"+ file.getName()+",文件大小是:"+ file.length());
                data.add(new Bean(file.getParentFile().getAbsolutePath(), file.getName(), (file.length())));
            } else {
                getFile(data, file.getAbsolutePath());
            }
        }
    }

    private static int getFileSize(File f) {
        InputStream fis = null;
        try {
            fis = new FileInputStream(f);
            int docsize=fis.available();
            return docsize;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

}

@Data
class Bean {
    private String filePath;
    private String fileName;
    private long size;

    public Bean(String filePath, String fileName, long size) {
        this.filePath = filePath;
        this.fileName = fileName;
        this.size = size;
    }
}

方法二:

        FileChannel fc= null;
        try {
            File f= new File("G:\\temp\\Stanford.zip");
            if (f.exists() && f.isFile()){
                FileInputStream fis= new FileInputStream(f);
                fc= fis.getChannel();
                System.out.println(fc.size());
            }else{
                System.out.println("file doesn't exist or is not a file");
            }
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            if (null!=fc){
                try{
                    fc.close();
                }catch(IOException e){
                    System.err.println(e);
                }
            }
        }

参考连接:1、https://www.cnblogs.com/woshuaile/p/8405261.html

2、https://blog.csdn.net/chaijunkun/article/details/22387305

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java 读取某文件下的所有文件名称以及大小,并输出在xls表格里 的相关文章

随机推荐

  • 将cookie字符串转化为json对象

    先看一下cookie长啥样 在控制台输入查看document cookie 如下图 分析可以看出cookie是以 key value key2 value2 的结构 好 接下来我们就将其转为json对象 function cookieToJ
  • base64 加密解密

    1 str lt gt bytes str bytes bytes s encoding utf8 bytes str str b encoding utf 8 此外还可通过编码解码的形式对二者进行转换 str 编码成 bytes 格式 s
  • Spring cloud+Zuul+JWT实现无状态统一身份认证和分布式限流

    本文主旨搭建一个无状态统一身份认证的系统 基于Spring cloud微服务架构 Eureka 实现服务的注册与发现 Zuul网关实现服务路由 请求过滤和限流功能 使用JWT规范实现客户登陆信息的服务端无状态话 相关文章参考 Spring
  • localStorage sessionStorage cookie的区别

    文章转载自 http blog csdn net cwzhsi article details 49557879 一 基本概念 Cookie cookie比较小 大小限制在4kb左右 是网景公司的前雇员 LouMontulli 在1993年
  • tilemap 菱形_【cocos3.x+tilemap】制作rpg小游戏(二)遮挡与碰撞

    前面制作了地图 现在就可以在工程中使用了 现在只实现了遮挡与碰撞 后续再实现点击屏幕移动 寻路算法 npc交互等 实现遮挡与碰撞 都需要解决一个核心问题 当前角色到底在哪一块瓦片上 也就是cocos坐标如何转化为TileMap瓦片坐标 这个
  • python函数中将变量名转换成字符串

    考虑到在日常中 常常需要对模型指标输出 但涉及多个模型的时候 需要对其有标示输出 故需要将模型变量名转换成字符串 看到的基本方法有两种 一 方法层面 方法1 函数内推荐 def namestr obj namespace return na
  • webpack.config.js基础配置(五大核心属性)

    在上一节webpack零基础入门中我们在安装完webpack 和 webpack cli依赖之后 直接通过npx webpack src main js mode development的方式对src下的js文件进行了打包 其中的 src
  • threejs 拖拽事件会触发点击事件

    记录three中拖拽场景会触发click事件 解决方法 dragNoClick gt const lock e gt 获取相机位置 this lockStart this map controls target clone const un
  • 深度学习框架以及过程----通俗解释

    这些框架是实现卷积神经网络的 深度学习框架就是帮助你深度学习的工具 就像铲子和锅一样 也有不同品牌 有caffe出品的 有facebook出品 有google出品 换言之这些就是一些库 一套深度学习框架 就是这个品牌的一套积木 各个组件就是
  • 组件(component)技术介绍

    转自 http blog csdn net touzani article details 1619472 组件 component 技术是各种软件重用方法中最重要的一种方法 也是分布式计算和Web服务的基础 网络应用中的软件组件 又被称为
  • vue+vant商品列表批量倒计时

    最近因为一个项目需要用到商品批量倒计时 当时使用vant封装好的组件CountDown编写 起初不知道 timeData 这个对象只需要传time的时间戳就可以自动生成 走了一大波弯路 现在想想也是醉了 最开始写这个倒计时的时候没有考虑到使
  • How to throw an error in MySql procedure?

    http stackoverflow com questions 4862911 how to throw an error in mysql procedure 9down votefavorite What is the mechani
  • vue中使用不同版本的Swiper

    因为随着swiper的版本更新 各种版本出现的写法有所不同 swiper3 npm install swiper 3 save dev 因为版本的更新 swiper3逐渐使用的越来越少 但是还是有很多不必要的坑 我们下载完之后只需要在全局m
  • Python 统计一个纯英文文件中的单词总个数

    import sys import string if len sys argv 1 or sys argv 1 in h help print usage countWord py filename 1 filename 2 finena
  • C++实现链式栈

    pragma once template
  • python可视化石头剪刀布界面

    python是一个简单高效的编程语言 其易于扩展和丰富的库深受人们的欢迎 它让编程变得更简单 易懂 现在它也可以用来写一个一个简单的小游戏 无需网络能玩一天 代码如下 import random import tkinter from tk
  • abb 机械手臂 示例程序

    教学设备的示例程序 如有侵权 立删 MODULE SX815Q1 CONST robtarget pHomeQ1 396 50 0 00 630 00 0 499991 0 500009 0 499998 0 500002 1 1 3 0
  • 算法实践1_线性回归

    参数解释 sklearn linear model LinearRegression fit intercept True normalize False copy X True n jobs None 超参 解释 类型 默认值 fit i
  • 生产API版本及SIMNOW环境说明

  • Java 读取某文件下的所有文件名称以及大小,并输出在xls表格里

    方法一 package com test demo test import lombok Data import java io import java util ArrayList import java util List Title