java 文件统计_Java中的文件读取与保存 之 统计文件夹及文件个数

使用递归来一层一层统计文件夹下的文件(夹)个数,大小,以及是否隐藏.

使用到的类:File

统计的方法

FileUnit.java

import java.io.File;

public class FileUnit {

static int filecount=0;

static int menucount=0;

static int size=0;

static long time=0;

/**

* 统计指定路径下的标准文件的个数

* @param path 路径

* @return

*/

public static void CountFile(String Path){

File file=new File(Path);

if(!file.exists()){

System.out.println("不存在");

return ;

}

File []fs=file.listFiles();//返回当前路径下的所有文件。

if (fs==null){

//file不是一个文件夹,是一个文件

size=(int) file.length();

time = file.lastModified();

filecount=1;

System.out.println(file.getAbsolutePath()+"最后修改时间为 "+time );

return ;

}

//运行到这里说明Path路径是一个文件夹

for(int i=0;i

File f=fs[i];

if(f.isFile()){

filecount++;

size += (int) f.length();

time = file.lastModified();

//将所有文件设为只读

//f.setReadOnly();

}else if(f.isDirectory()){

//调用本身

menucount++;

System.out.println(f.getAbsolutePath());

CountFile(f.getAbsolutePath());

}

}

}

}

窗口:

UI.java

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class UI extends JFrame implements ActionListener{

JLabel jInclude=new JLabel("包含:\t0个文件,0个文件夹");

JLabel jclass=new JLabel("类型:\t文件");

JLabel jSize=new JLabel("大小:\t0KB(0字节)");

JLabel jTime=new JLabel("修改时间:\t2014年5月21日,18:36:01");//未实现

JLabel jPath=new JLabel("位置:\t");

JLabel jProperties=new JLabel("属性: 隐藏? ");

JButton choose = new JButton("选择");

FileUnit f1=new FileUnit();

JFileChooser jfc = new JFileChooser("E:\\");

static char hidden='否';

public static void main(String[] args) {

UI ui=new UI();

//String s="E:\\hb\\Test";

//f1.CountFile(s);

ui.init();

ui.initWighet();

ui.setVisible(true);

}

public void init(){

this.setSize(300,500);

this.setDefaultCloseOperation(3);

this.setTitle("文件统计器");

this.setLayout(null);

}

public void initWighet(){

jclass.setBounds(20, 20, 200, 20);

jPath.setBounds(20, 50, 200, 20);

jSize.setBounds(20, 80, 200, 20);

jInclude.setBounds(20, 110, 200, 20);

jTime.setBounds(20, 140, 300, 20);

jProperties.setBounds(20, 180, 200, 40);

choose.setBounds(20, 300, 100, 40);

choose.addActionListener(this);

this.add(jclass);

this.add(jPath);

this.add(jSize);

this.add(jInclude);

this.add(jTime);

this.add(jProperties);

this.add(choose);

}

public void Test(String s){

File f =new File(s);

f1.CountFile(s);

if(f.isHidden()){

hidden='是';

}else{

hidden='否';

}

jPath.setText(s);

jSize.setText("大小:\t"+FileUnit.size/1024+"KB("+FileUnit.size+"字节)");

jProperties.setText("属性: 隐藏? "+hidden);

jInclude.setText("包含:\t"+FileUnit.filecount+"个文件,"+FileUnit.menucount+"个文件夹");

if(FileUnit.menucount>0){

jclass.setText("类型:\t文件夹");

}

}

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if(source.equals(choose)){

int state = jfc.showOpenDialog(this);

if (state == 0) {// 点击了打开按钮

String str=jfc.getSelectedFile().getAbsolutePath();

System.out.println(str);

Test(str);//JFileChooser只能选择文件不能选择文件夹.

}

}

}

}

遇到的问题:

1.最后一次修改的时间用file.lastModified();返回一个long.不知道怎么换成时间的格式

2.用JFileChooser无法选择文件夹.如果在程序中定义路径可以统计文件夹个数以及文件总大小.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值