java 怎么读取指定路径下所有文件名
1个回答
展开全部
public class GetFileName
{
public static String [] getFileName(String path)
{
File file = new File(path);
String [] fileName = file.list();
return fileName;
}
public static void getAllFileName(String path,ArrayList<String> fileName)
{
File file = new File(path);
File [] files = file.listFiles();
String [] names = file.list();
if(names != null)
fileName.addAll(Arrays.asList(names));
for(File a:files)
{
if(a.isDirectory())
{
getAllFileName(a.getAbsolutePath(),fileName);
}
}
}
public static void main(String[] args)
{
String [] fileName = getFileName("F:\\xiaoshuo");
for(String name:fileName)
{
System.out.println(name);
}
System.out.println("--------------------------------");
ArrayList<String> listFileName = new ArrayList<String>();
getAllFileName("F:\\xiaoshuo",listFileName);
for(String name:listFileName)
{
System.out.println(name);
}
}
}
{
public static String [] getFileName(String path)
{
File file = new File(path);
String [] fileName = file.list();
return fileName;
}
public static void getAllFileName(String path,ArrayList<String> fileName)
{
File file = new File(path);
File [] files = file.listFiles();
String [] names = file.list();
if(names != null)
fileName.addAll(Arrays.asList(names));
for(File a:files)
{
if(a.isDirectory())
{
getAllFileName(a.getAbsolutePath(),fileName);
}
}
}
public static void main(String[] args)
{
String [] fileName = getFileName("F:\\xiaoshuo");
for(String name:fileName)
{
System.out.println(name);
}
System.out.println("--------------------------------");
ArrayList<String> listFileName = new ArrayList<String>();
getAllFileName("F:\\xiaoshuo",listFileName);
for(String name:listFileName)
{
System.out.println(name);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询