读取出某个文件夹下面的所有某后缀名的文件
String filePath = “某个目录”;
File file = new File(filePath);
FilterBySuffix filter = new FilterBySuffix(".deb");
String[] files = file.list(filter);
int counter = 0;
for (String filename:files){
int versionStartIndex = filename.indexOf("_");
int versionEndIndex = filename.indexOf("-",versionStartIndex);
if( versionStartIndex == -1 || versionEndIndex == -1 ){
continue;
}
String version = filename.substring(versionStartIndex + 1,versionEndIndex);
AptDO aptDO = new AptDO();
aptDO.setPackageName(filename);
aptDO.setVersion(version);
aptDO.setPackagePath(esbConfig.getPackagePath() + filename);
if(aptService.save( aptDO ) > 0){
counter ++;
}
}
Class FilterBySuffix
package com.esb.common.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.File;
import java.io.FilenameFilter;
@Data
@AllArgsConstructor
public class FilterBySuffix implements FilenameFilter {
private String suffix;
@Override
public boolean accept(File dir, String name) {
return name.endsWith(suffix);
}
}
|