疯狂Java讲义习题11.2

来源:互联网 发布:百度软件助手怎么样? 编辑:程序博客网 时间:2024/06/05 03:23
题目描述:
定义一个工具类,该类要求用户运行该程序时输入一个路径。该工具类会将该路径下的文件,文件夹的数量统计出来。
代码如下:(有待改进)

点击(此处)折叠或打开

  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class Hello {
  4.     //private static String pa
  5.     public static void recv(String path)
  6.     {
  7.         File newFile = new File(path);
  8.         if (newFile.exists())
  9.         {
  10.             File[] fileList = newFile.listFiles();
  11.             if (fileList.length == 0)
  12.             {
  13.                 System.out.println("文件夹是空的");
  14.             }
  15.             int countName = 0,countPath = 0;
  16.             for (File file:fileList)
  17.             {
  18.                 
  19.                 if (file.isFile())
  20.                 {
  21.                     System.out.println("文件名"+file.getAbsolutePath());
  22.                     countName++;
  23.                 }
  24.                 else{
  25.                     System.out.println("路径名"+file.getAbsolutePath());
  26.                     countPath++;
  27.                     recv(file.getAbsolutePath());
  28.                 }
  29.             }    
  30.             //System.out.println("文件名"+file.getAbsolutePath()+countName+"路径名"+file.getAbsolutePath()+countPath);
  31.             System.out.println("countNmae = "+countName+"countPath = "+countPath);
  32.         }
  33.         else
  34.             System.out.println("文件不存在");
  35.     }
  36.     public static void main(String[] args)throws Exception
  37.     {
  38.         Scanner sc = new Scanner(System.in);
  39.         //File newFile = new File("c:");
  40.         //File newFile = new File(path);
  41.         while (sc.hasNext())
  42.         {
  43.             
  44.             recv(sc.next());
  45.             
  46.         }
  47.         
  48.     }
  49.     
  50.     
  51. }
运行结果:
F:\冰点文库
文件名F:\冰点文库\11-论文综述.docx
文件名F:\冰点文库\11文献阅读成绩报告单.doc
文件名F:\冰点文库\11研究生开题申请表.doc
文件名F:\冰点文库\Google.ProtocolBuffers.dll
路径名F:\冰点文库\images
文件名F:\冰点文库\images\ttt
countNmae = 1countPath = 0
文件名F:\冰点文库\Update.exe
文件名F:\冰点文库\Update.exe.CodeAnalysisLog.xml
文件名F:\冰点文库\Update.exe.lastcodeanalysissucceeded
文件名F:\冰点文库\Update.pdb
文件名F:\冰点文库\UrlManager_Win32.exe
文件名F:\冰点文库\UrlManager_Win32.pdb
文件名F:\冰点文库\UrlManager_Win32.vshost.exe
文件名F:\冰点文库\UrlManager_Win32.vshost.exe.manifest
文件名F:\冰点文库\开题报告Final.doc
文件名F:\冰点文库\开题报告评审表.doc
文件名F:\冰点文库\论文工作计划.doc
countNmae = 15countPath = 1
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(93) | 评论(0) | 转发(0) |
0

上一篇:疯狂java讲义习题11.1

下一篇:疯狂Java讲义习题11.3

相关热门文章
  • Tomcat 6 配置SSI
  • 让Resin支持shtml(SSI)- 静...
  • tomcat + ssi
  • ASP JavaScript Lessons(8-14)
  • JDK1.6官方下载_JDK6官方下载_...
给主人留下些什么吧!~~
原创粉丝点击