Chinaunix首页 | 论坛 | 博客
  • 博客访问: 502895
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: Java

2016-12-03 15:32:09

题目描述:
定义一个工具类,该类要求用户运行该程序时输入一个路径。该工具类会将该路径下的文件,文件夹的数量统计出来。
代码如下:(有待改进)

点击(此处)折叠或打开

  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
阅读(1086) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~