//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();
}
}