JAVA 遍历C盘下所有TXT格式文件

本文介绍如何使用Java编程语言遍历C盘下所有TXT格式文件,包括文件路径设置、创建目录与文件的基本操作,以及遍历查找特定类型的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//set classpath=C:\Users\heqing\java
//javac C:\Users\heqing\java\输入输出流测试\输入输出流测试_1\文件遍历.java
//java  输入输出流测试.输入输出流测试_1.文件遍历.java

 

package 输入输出流测试.输入输出流测试_1;
import java.io.File;

public class 文件遍历 {
    File myPath = new File("c:\\");      // file类可以保存路径          
    // File bookPath = new File(myPath,"book");
    // File programePath = new File(myPath,"programe");
    // File myFile = new File("c:\\Java\\myFile.txt");
    // File fileEx = new File(myPath,"FileEx.Java");
    // File calculator = new File(myPath,"Calculator.Java");
    // public PrintFolder() {          
       // if(!myPath.exists()){myPath.mkdir();}
      //  if(!bookPath.exists()){bookPath.mkdir();}
      //  if(!programePath.exists()){programePath.mkdir();}
      //  try
     //   {
     //      if(!myFile.exists()){myFile.createNewFile();}
     ///      if(!fileEx.exists()){fileEx.createNewFile();}
     //      if(!calculator.exists()){calculator.createNewFile();}
    //    }
     //  catch(Exception e)
    //   {
    //       e.printStackTrace();
    //    }
   ///  } 
    public void go(){
       String folderArray[];
       folderArray = myPath.list(); //自动遍历每一个一级子目录文件  遍历一次保存至字符串数组中
       System.out.println("C盘下TXT文件为:");
     for(int i=0;i<folderArray.length;i++){
           String m=folderArray[i];
     if(m.length()>3)
      {
              if( ( m.substring((m.length()-3),m.length()) ).equals("txt")   )
         {
                  System.out.println(folderArray[i]);
               }  
  
              }
      }  
 }
   public static void main(String[] args) {
        文件遍历 pf = new 文件遍历();
        pf.go();

     }
  }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值