日期:2014-05-20  浏览次数:20800 次

百分求助,如何获取目录下的所有文件??????????????
如何取得c:\test这个目录下的所有文件呢,取得的显示方式如下,用控制台输入即可

文件名    文件大小  创建日期  
test.txt        20M               2007-09-09
tfaw.txt        20M               2007-09-09
dfed.txt        20M               2007-09-09
awfe.txt        20M               2007-09-09
....

------解决方案--------------------
File f=new File( "c:\\ ");;
File files[]=f.listFiles(new FileFilter(){
public boolean accept(File pathname) {
return pathname.isFile();
}
});
for(int i=0;i <files.length;i++){
File tempfile=files[i];
System.out.println(tempfile.getName()+ " \t "+tempfile.length()+ " \t "+new Date(tempfile.lastModified()));
}
------解决方案--------------------
public static void deleteSrcPicture(String s_path)
{
try
{
File s_file = new File( s_path.toString() );
if (!s_file.exists())
{
System.out.println( "s_file is not a directory. " );
return;
}
File[] files = s_file.listFiles();
for (int i = 0; i < files.length; i++)
{
//System.out.println( files[i].getName() );
logger.info( i + "== " + files[i].getName() );
String fileName = files[i].getName();
/*******************************************************************************************************
* if directory,circulate
******************************************************************************************************/
if (files[i].isDirectory())
{
StringBuffer s_subPath = new StringBuffer( s_path.toString() );
s_subPath.append( System.getProperty( "file.separator " ) );
s_subPath.append( files[i].getName() );
deleteSrcPicture( s_subPath.toString() );
}
else if (files[i].isFile())
{
//处理你需要的逻辑
}
}
catch (Exception ex)
{
logger.info( ex );
}
------解决方案--------------------
好。
------解决方案--------------------
//file.java

package com.shlq.sample;

import java.io.File;
import java.io.FileFilter;
import java.util.Date;

public class file {
public static void main(String[] args) {
System.out.println( "--The Files List-- ");
File file = new File( "c:\\test\\ ");
;
File files[] = file.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isFile();
}
});
for (int i = 0; i < files.length; i++) {
File temp = files[i];
System.out.println(temp.getName() + " \t " + temp.length()
+ " \t " + new Date(temp.lastModified()));
}
}
}

/*
the files list
file_a.doc 10752 Tue Jun 19 11:17:28 CST 2007
file_b.doc 10752 Tue Jun 19 11:17:28 CST 2007
file_c.doc 10752 Tue Jun 19 11:17:28 CST 2007
*/
------解决方案--------------------