假定有若干特定名称文件放在指定目录下。如何写一个过程,只要输入目录名,即可打开下面这些特定名称的文件??
假设文件名为index.dat,content.dat,history.dat,目录为fleParentFolder形如vbscript伪码
class CLib
public fleIndex,fleContent,fleHistory
public sub constructor(fleParentFolder)
set fleIndex=fleParentFolder.files("index.dat");
set fleContent=fleParentFolder.files("content.dat");
set fleHisotry=fleParentFolder.files("history.dat");
end sub
end class
或者形如我的烧饼java做法
class CLib
public File fleIndex,fleContent,fleHistory;
public CLib(File fleParent){
String strAbs=fleParent.getAbsolutePath();
fleIndex=new File(strAbs + File.pathSeparator + "index.dat");
fleContent=new File(strAbs + File.pathSeparator + "content.dat");
...
}
end class 有没有更简单的,直接的办法??
假设文件名为index.dat,content.dat,history.dat,目录为fleParentFolder形如vbscript伪码
class CLib
public fleIndex,fleContent,fleHistory
public sub constructor(fleParentFolder)
set fleIndex=fleParentFolder.files("index.dat");
set fleContent=fleParentFolder.files("content.dat");
set fleHisotry=fleParentFolder.files("history.dat");
end sub
end class
或者形如我的烧饼java做法
class CLib
public File fleIndex,fleContent,fleHistory;
public CLib(File fleParent){
String strAbs=fleParent.getAbsolutePath();
fleIndex=new File(strAbs + File.pathSeparator + "index.dat");
fleContent=new File(strAbs + File.pathSeparator + "content.dat");
...
}
end class 有没有更简单的,直接的办法??
try {
//run.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe http://www.qq.com/");
run.exec("D:\\Program Files\\Tencent\\QQ\\QQ.exe");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
不用listFilter过滤,不用path拼装
File[] list=path.listFiles();
for(File file:list){
System.out.println(file.getName());
}
/**
* List the files under root dir
* @param root
* @param fileSuffixName
* which kind of file will be list.
* example: xml txt...
* @param excludeDirName
* which subDir's file will be excluded.
* @return
*/
public List<File> getFileList(File root,String fileSuffixName,String excludeDirName) {
File[] filesOrDirs = root.listFiles();
for (int i = 0; i < filesOrDirs.length; i++) {
if (filesOrDirs[i].isDirectory()) {
// comfirm that is not backup history dir
if (!excludeDirName.equals(filesOrDirs[i].getName())) {
getFileList(filesOrDirs[i],fileSuffixName,excludeDirName);
}
} else {
if(fileSuffixName.equalsIgnoreCase(this.getFileSuffixName(filesOrDirs[i])))
result.add(filesOrDirs[i]);
}
}
return result;
}
// involk
getFileList(new File("fleParentFolder"),"dat","");正好我有个现成的,函数不满意你可以改改
import java.io.File;
import java.util.HashMap;public class LoadFiles {
private HashMap<String,File> filesMap=new HashMap<String,File>(); //存放文件对象的MAP
private String[] fileType=new String[]{"index.dat","content.dat","history.dat"}; //需要读取的文件
/**
* 读取文件
* @param path
* @return
*/
public LoadFiles(String path){
File root=new File(path);
File[] dirs=root.listFiles(); //获取目录下所有文件
for(int i=0;i<fileType.length;i++){
for(int j=0;j<dirs.length;j++){
String str=dirs[j].getName(); //文件名
if(str.equals(fileType[i])){ //找到文件,放入HASHMAP中
String filename=str.substring(0,str.lastIndexOf(".")); //获取文件名,没有扩展名
File file=new File(dirs[j].getPath()); //获取文件对象
filesMap.put(filename, file);
break;
}
}
}
}
/**
* 获取一个文件对象
* @param fileName
* @return
*/
public File getFile(String fileName){
return filesMap.get(fileName);
}
/**
* 获取所有文件对象的HASHMAP
* @return
*/
public HashMap<String,File> getFilesMap(){
return filesMap;
}
public static void main(String[] args){
LoadFiles loadFiles=new LoadFiles("F:\\test\\fleParentFolder");
File fleIndex = loadFiles.getFile("index");
File fleContent = loadFiles.getFile("content");
File fleHistory = loadFiles.getFile("history");
}
}