zoukankan      html  css  js  c++  java
  • 磁盘文件遍历

    package test.WinTraversal;
    import java.io.File;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.FileOutputStream;
    import java.io.FileDescriptor;
    /**
     * 遍历磁盘文件并将所有的文件名保存至文本
     * @author jsh

     * @version 1.0
     */
    public class WinTraversal
    {
        private static int countFile = 0;
        private static int countDir = 0;
     //遍历磁盘文件,用于输出到文件
     public void service(File root,String path) throws Exception
     {
        int i=0;
        System.out.println("---目录-----"+path+"//-----下的文件有:");
        String names[]= root.list();
        if(names!=null)
        {       
           for(i=0;i<names.length;i++)
              {
               String f1Path = path+"
    //"+names[i];
                         
               File f1=new File(f1Path);
            
                  if(f1.isDirectory())
                    {
                        countDir++;                                 
                     service(f1,f1Path);  
                    }                                  
                  else
                  {
                   System.out.println("文件"+(i+1)+":"+names[i]); 
                   countFile++;
                  }  
           }       
        } 
     }
     public static void main(String sgrs[])
     {
         System.out.print("输入要搜索的磁盘盘符:");
      try
      {
         //从控制台接收用户输入的数据
         BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
            String input = cin.readLine();
            //创建文件对象
            String path = input+"/";
            File file = new File(path);
            //创建保存数据的文本文件
           File test = new File("fileNameList.txt"); 
          
            //重定向输出流至文件
              PrintStream  out = new PrintStream(new FileOutputStream(test));
              System.setOut(out);
               //执行遍历磁盘文件的方法
        new WinTraversal().service(file,path);
        //重定向至标准输出流
        System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)) );
        System.out.println("---任务成功完成!---");
        System.out.println(input+"盘中共有"+countDir+"个文件夹");
        System.out.println(input+"盘中共有"+countFile+"个文件");
         //获得源文件所在路径,默认将txt文件保存在源文件位置
        String textPath = System.getProperty("user.dir").toString()+"
    //"+"fileNameList.txt";
        System.out.println("textPath = "+textPath);
        //打开文件
        Runtime rt=Runtime.getRuntime();
        rt.exec("notepad "+textPath);//打开文本文档
      }
      catch(Exception e)
      {
       System.out.println("---程序发生错误!任务失败!---");
       e.printStackTrace();
      }
     }
    }

     

  • 相关阅读:
    对于数据的测试
    绕过前端,直接调用后端接口的可能性
    API接口自动化之3 同一个war包中多个接口做自动化测试
    API接口自动化之2 处理http请求的返回体,对返回体做校验
    API接口自动化之1 常见的http请求
    DB中字段为null,为空,为空字符串,为空格要怎么过滤取出有效值
    Linux 常用的压缩命令有 gzip 和 zip
    SQL 常用的命令
    JVM内存管理的机制
    Linux 常见命令
  • 原文地址:https://www.cnblogs.com/shihujiang/p/2396390.html
Copyright © 2011-2022 走看看