Java 遍历磁盘文件并将所有的文件名保存至文本


package jack.joy;

 

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 Jack
 * @version 1.0
 *
 */
 
public class ListFileName
{
    private static int countFile = 0;
    private static int countDir = 0;
/*   
    //在控制台打印出文件名字列表 的方法
 public void display(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++;                                   
                 display(f1,f1Path);
                 System.out.println("-----------------------------"); 
                }                                  
              else  
                { 
                   countFile++;   
                   System.out.println("文件名"+(i+1)+":"+names[i]);  
                }     
       }  
    
    }
       
 }
*/ 
 //遍历磁盘文件,用于输出到文件
 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 ListFileName().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";
   
    //打开文件
    Runtime rt=Runtime.getRuntime();
    rt.exec("notepad "+textPath);//打开文本文档
  
    
          // System.out.println(this.getAbsolutePath());
  }
  catch(Exception e)
  {
   System.out.println("---程序发生错误!任务失败!---");
   e.printStackTrace();
  }
      
 }
 
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值