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

请教关于如何读取文件夹中指定文件的问题
我得问题是这样的:比如说有个文件夹,文件夹中还有其他子文件夹,每个子文件夹中都有一个info.xls文件,java上是如何实现读取该文件夹下所有的info.xls文件的?  
我的理想方案是这样的,提交一个文件夹目录,程序就读取该文件夹下所有的info.xls文件,然后作相关处理
源代码是这样的:
public   void   importExcel(String   file)   throws   SQLException   {  
Connection   con   =   null;  
CallableStatement   cstmt   =   null;  
ResultSet   rs   =   null;  
POIFSFileSystem   fs   =   null;  
HSSFWorkbook   wb   =   null;  
try   {  
fs   =   new   POIFSFileSystem(new   FileInputStream(file));  
wb   =   new   HSSFWorkbook(fs);  
}   catch   (IOException   e)   {  
e.printStackTrace();  

}  
HSSFSheet   sheet   =   wb.getSheetAt(0);  
HSSFRow   row   =   null;  
HSSFCell   cell   =   null;  
String   name   =   " ";  
double   value;  
int   rowNum,   cellNum;  
int   i;  
rowNum   =   sheet.getLastRowNum();  
for   (i   =   0;   i   <=   rowNum;   i++)   {  
row   =   sheet.getRow(i);  
//cellNum   =   row.getLastCellNum();  
cell   =   row.getCell((short)   0);  
name   =   cell.getStringCellValue();  
cell   =   row.getCell((short)   1);  


value   =   cell.getNumericCellValue();  


try   {  
con   =   DBConnection.getConnection();  
cstmt   =   con.prepareCall(IConstants.SP_IMPORTEXCEL_KEY);  
cstmt.setString(1,   name);  
cstmt.setDouble(2,   value);  
cstmt.execute();  
}   catch   (SQLException   e)   {  
e.printStackTrace();  
logger.error(e.getMessage());  
throw   e;  
}   finally   {  
try   {  

if   (cstmt   !=   null)   {  
cstmt.close();  
}  
if   (con   !=   null)   {  
con.close();  
}  
}   catch   (SQLException   e)   {  
e.printStackTrace();  
logger.error(e.getMessage());  
throw   e;  
}  

}  


}  

}  

}  
以上是我写的源代码,现在只能实现提交某个xls文件,然后作相应的导数据可操作,可是现在我的xls文件很多,而且分布在很多文件夹中,所以想作一个批处理,读取文件夹下所有的info.xls文件,希望哪位大虾可以指点一二  


------解决方案--------------------
指定一个根目录然后list出它所有的子目录(listFiles),然后循环判断每个文件,如果isFile就读取,如果isDirectory就递归上面的过程.
------解决方案--------------------
我是菜鸟,上面的朋友能不能讲的详细点

------解决方案--------------------
递归判断 直到没有子文件夹为止
------解决方案--------------------
要用递归

File rf = new File( "your root path ");
this.getAllFiles(rf);

private void getAllFiles(File rf){

File[] fs = rf.listFiles();

for(int i=0;i <fs.length;i++ ){
if( fs[i].isFile() ){//如果是文件,看看是不是自己要找的文件
if(fs[i].getName.equals( "you file name ")){
system.out.println( "get one your need ");
}