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


  • 博客分类: 
  • Java
 

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

 

使用到的类:File

 

统计的方法

FileUnit.java

Java代码   收藏代码
  1. import java.io.File;  
  2.   
  3. public class FileUnit {  
  4.     static int filecount=0;  
  5.     static int menucount=0;  
  6.     static int size=0;  
  7.     static long time=0;  
  8.       
  9.     /** 
  10.      * 统计指定路径下的标准文件的个数 
  11.      * @param path 路径 
  12.      * @return 
  13.      */  
  14.     public static void CountFile(String Path){  
  15.         File file=new File(Path);  
  16.           
  17.           
  18.         if(!file.exists()){  
  19.             System.out.println("不存在");  
  20.             return ;  
  21.         }  
  22.           
  23.           
  24.         File []fs=file.listFiles();//返回当前路径下的所有文件。  
  25.         if (fs==null){  
  26.             //file不是一个文件夹,是一个文件  
  27.             size=(int) file.length();  
  28.             time = file.lastModified();  
  29.             filecount=1;  
  30.   
  31.             System.out.println(file.getAbsolutePath()+"最后修改时间为 "+time );  
  32.             return ;  
  33.         }  
  34.           
  35.         //运行到这里说明Path路径是一个文件夹  
  36.         for(int i=0;i<fs.length;i++){  
  37.             File f=fs[i];  
  38.             if(f.isFile()){  
  39.                 filecount++;  
  40.                 size += (int) f.length();  
  41.                 time = file.lastModified();  
  42.   
  43.                   
  44.                 //将所有文件设为只读  
  45.                 //f.setReadOnly();  
  46.             }else if(f.isDirectory()){  
  47.                 //调用本身  
  48.                 menucount++;  
  49.                 System.out.println(f.getAbsolutePath());  
  50.                 CountFile(f.getAbsolutePath());  
  51.   
  52.             }             
  53.         }         
  54.     }  
  55.   
  56. }  

 

 

 

窗口:

UI.java

 

Java代码   收藏代码
  1. import java.awt.event.ActionEvent;  
  2. import java.awt.event.ActionListener;  
  3. import java.io.File;  
  4.   
  5. import javax.swing.JButton;  
  6. import javax.swing.JFileChooser;  
  7. import javax.swing.JFrame;  
  8. import javax.swing.JLabel;  
  9.   
  10.   
  11. public class UI extends JFrame implements ActionListener{  
  12.     JLabel jInclude=new JLabel("包含:\t0个文件,0个文件夹");  
  13.     JLabel jclass=new JLabel("类型:\t文件");  
  14.     JLabel jSize=new JLabel("大小:\t0KB(0字节)");  
  15.     JLabel jTime=new JLabel("修改时间:\t2014年5月21日,18:36:01");//未实现  
  16.     JLabel jPath=new JLabel("位置:\t");  
  17.     JLabel jProperties=new JLabel("属性:       隐藏? ");  
  18.     JButton choose = new JButton("选择");  
  19.     FileUnit f1=new FileUnit();  
  20.     JFileChooser jfc = new JFileChooser("E:\\");  
  21.     static char hidden='否';  
  22.       
  23.     public static void main(String[] args) {  
  24.           
  25.         UI ui=new UI();  
  26. //      String s="E:\\hb\\Test";  
  27. //      f1.CountFile(s);  
  28.         ui.init();  
  29.         ui.initWighet();  
  30.         ui.setVisible(true);  
  31.           
  32.         }  
  33.       
  34.     public void init(){  
  35.         this.setSize(300,500);  
  36.         this.setDefaultCloseOperation(3);  
  37.         this.setTitle("文件统计器");  
  38.         this.setLayout(null);  
  39.     }  
  40.       
  41.     public void initWighet(){  
  42.           
  43.         jclass.setBounds(202020020);  
  44.         jPath.setBounds(205020020);  
  45.         jSize.setBounds(208020020);  
  46.         jInclude.setBounds(2011020020);  
  47.         jTime.setBounds(2014030020);  
  48.         jProperties.setBounds(2018020040);  
  49.         choose.setBounds(2030010040);  
  50.         choose.addActionListener(this);  
  51.           
  52.         this.add(jclass);  
  53.         this.add(jPath);  
  54.         this.add(jSize);  
  55.         this.add(jInclude);  
  56.         this.add(jTime);  
  57.         this.add(jProperties);  
  58.         this.add(choose);  
  59.     }  
  60.       
  61.     public void Test(String s){  
  62.           
  63.           
  64.         File f =new File(s);  
  65.         f1.CountFile(s);  
  66.         if(f.isHidden()){  
  67.             hidden='是';  
  68.         }else{  
  69.             hidden='否';  
  70.         }  
  71.         jPath.setText(s);  
  72.         jSize.setText("大小:\t"+FileUnit.size/1024+"KB("+FileUnit.size+"字节)");  
  73.         jProperties.setText("属性:       隐藏? "+hidden);  
  74.         jInclude.setText("包含:\t"+FileUnit.filecount+"个文件,"+FileUnit.menucount+"个文件夹");  
  75.         if(FileUnit.menucount>0){  
  76.             jclass.setText("类型:\t文件夹");  
  77.         }  
  78.           
  79.     }  
  80.   
  81.     public void actionPerformed(ActionEvent e) {  
  82.         Object source = e.getSource();  
  83.         if(source.equals(choose)){  
  84.             int state = jfc.showOpenDialog(this);  
  85.             if (state == 0) {// 点击了打开按钮  
  86.                 String str=jfc.getSelectedFile().getAbsolutePath();  
  87.                 System.out.println(str);  
  88.                 Test(str);//JFileChooser只能选择文件不能选择文件夹.  
  89.             }  
  90.               
  91.         }  
  92.           
  93.           
  94.     }  
  95. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值