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 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 listFileName =
new
ArrayList();
getAllFileName(
"F:\\xiaoshuo"
,listFileName);
for
(String name:listFileName)
{
System.out.println(name);
}
}
}
Linux命令行参数数目是有限的,执行ls *.gz时,当通配符展开后,ls命令的参数列表超长了,导致运行出错。
如果只是想得到当前目录下(不包括子目录)的相关文件时,可以这样:
1
|
ls -1 | grep
".gz$"
> 1.txt
|
如果想得到当前目录下,包括子目录中的相关文件时,应该用find命令:
1
|
find . -type f -name
"*.gz"
> 1.txt
|
#!/bin/sh
cd /home/gbase/XL_WORK/data/dns/20160125/16/;
/*获取目录下的所有文件名称,保存到一个变量中去*/
file_name_tmp=`ls /home/gbase/XL_WORK/data/dns/20160125/16/`;
/*将这个变量中保存的文件名称转换为数组*/
file_names=($(echo $file_name_tmp));
/*遍历这个数组*/
for file_name in ${file_names[@]}
do
echo $file_name
/*等待2秒*/
sleep 2s
done