使用递归来一层一层统计文件夹下的文件(夹)个数,大小,以及是否隐藏.
使用到的类: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<fs.length;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只能选择文件不能选择文件夹.
- }
- }
- }
- }