public static void listAll(String dir) { File file=new File(dir);
if(file.isFile()) System.out.println("There is a file nameed: "+file.getAbsolutePath()); else if(file.isDirectory()) { String[]dirname=file.list(); for(int i=0;i<dirname.length;i++) listAll(dirname[i]); }
}
public static void main(String[] args) { listAll("C:\\Users\\S\\Desktop"); }}
else if (file.isDirectory()) { String[] dirname = file.list(); for (int i = 0; i < dirname.length; i++) { listAll(dirname[i]); } System.out.println("There is a file named: " + file.getAbsolutePath()); }文件夹没有对应的打印信息,增加如上代码的输出语句就可以了
LZ想通过递归得到路径下所有文件名(包括子路径下的), 但是file.list()只能取到一组不完整的路径名,所以下面无法递归下去 ------------------------------------------------------------ Names denoting the directory itself and the directory's parent directory are not included in the result. Each string is a file name rather than a complete path.
这样改大概可以public static void main(String[] args) { listAll("F:\\Backup\\Multi-Media"); } public static void listAll(String dir) { File file = new File(dir); if (file.isFile()) { System.out.println("There is a file nameed: " + file.getAbsolutePath()); } else if (file.isDirectory()) { String[] dirname = file.list(); String[] completeName = new String[dirname.length]; for (int i = 0; i < dirname.length; i++) { completeName[i] = file.getAbsolutePath() + "\\" + dirname[i]; // System.out.println(completeName[i]); listAll(completeName[i]); } } }
import java.io.*;
public class selfList {
public static void listAll(String dir)
{
File file=new File(dir);
if(file.isFile())
System.out.println("There is a file nameed: "+file.getAbsolutePath());
else if(file.isDirectory())
{
String[]dirname=file.list();
for(int i=0;i<dirname.length;i++)
listAll(dirname[i]);
}
}
public static void main(String[] args) {
listAll("C:\\Users\\S\\Desktop"); }}
String[] dirname = file.list();
for (int i = 0; i < dirname.length; i++)
{
listAll(dirname[i]);
}
System.out.println("There is a file named: " + file.getAbsolutePath());
}文件夹没有对应的打印信息,增加如上代码的输出语句就可以了
但是file.list()只能取到一组不完整的路径名,所以下面无法递归下去
------------------------------------------------------------
Names denoting the directory itself and the directory's parent directory are not included in the result. Each string is a file name rather than a complete path.
listAll("F:\\Backup\\Multi-Media");
} public static void listAll(String dir) {
File file = new File(dir);
if (file.isFile()) {
System.out.println("There is a file nameed: "
+ file.getAbsolutePath());
} else if (file.isDirectory()) {
String[] dirname = file.list();
String[] completeName = new String[dirname.length];
for (int i = 0; i < dirname.length; i++) {
completeName[i] = file.getAbsolutePath() + "\\" + dirname[i];
// System.out.println(completeName[i]);
listAll(completeName[i]);
} } }
file.getAbsolutePath());可以改为file.getName());可能才是真正想要的