import java.io.File;
import java.io.FilenameFilter;
public class Test {
public static void main(String[] args) {
//C:盘下的所有文件,包括文件夹
String[] filenames = new File("C:/").list();
//C:盘下后缀名为txt的文件
String[] filenames2 = new File("C:/").list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
});
//C:盘下所有目录
String[] filenames3 = new File("C:/").list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return new File(dir, name).isDirectory();
}
});
}
}
两句代码就行了吧
File file_path=new File("E:\\Media\\");
File files[]=file_path.listFiles();
import java.io.File;
File file = new File(pathname);
String[] fileNames = file.list();
String path=“C:/”;
File file=new File(path);
File files=file.Files();
String []filesName=new String[files.length];
int index=0;
for(File f:files){
files[index]=f.getName();
index++;
}