Java 如何讀取目錄下的文件和目錄?
import java.io.File;
//讀取目
public class ReadDir{
public static void main(String[] args) {
String dirName = "java";
File file = new File(dirName);
if(file.isDirectory()) {
System.out.println("訪問(wèn)目錄"+dirName);
String s[] = file.list();
for(int i = 0;i<s.length;i++) {
File tempFile = new File(dirName+"/"+s[i]);
if(tempFile.isDirectory()) {
System.out.println(s[i]+"是一個(gè)目錄");
}else {
System.out.println(s[i]+"是一個(gè)文件");
}
}
}else {
System.out.println(dirName+"不是一個(gè)目錄");
}
}
}
點(diǎn)擊加載更多評(píng)論>>