我正在从共享地图网络驱动器目录中读取文件。逐行读取文件以登录文件夹并插入数据库然后删除。我得到一些文件多次记录相同的数据,但其他文件很好。为什么记录多次?关闭缓冲区或文件读取或解析方法有任何错误吗?
public static void main(String[] args) {
while (true) {
DirectoryFetcher fetch = new DirectoryFetcher();
if (fetch.hasFile()) {
for(File file:fetch.getFiles()){
try {
if(file.isFile()){
boolean flag = fetch.read(file);
if(flag){
file.delete();
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
}
}
}
public class DirectoryFetcher {
public DirectoryFetcher() {
ServerParams params = Configuration.getConfig().getServerParams();
directory = new File(params.getLocalDirectory());
if (directory.isDirectory()) {
files = directory.listFiles();
}else{
logger.info("Map Network Drive is not Exist");
}
}
boolean read(File file){
BufferedReader br = null;
String line = null;
boolean flag = false;
StringBuffer appendMessage = null;
String completeMessage = null;
try {
if(file.exists()){
br = new BufferedReader(new FileReader(file));
appendMessage = new StringBuffer(16384);
while ((line = br.readLine()) != null) {
appendMessage.append(line);
appendMessage.append('\n');
}
if(appendMessage != null){
completeMessage = appendMessage.toString();
}
if (completeMessage != null) {
if (!completeMessage.equals("")) {
flag = new BioRad().parse(completeMessage,file);
}
}
}
} catch (FileNotFoundException e) {
logger.error(e.getMessage());
e.printStackTrace();
}catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
}
finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
return flag ;
}
}
public boolean parse(String message,File file) {
TestResultParser parser = null;
String[] records = message.split(RECORD_SPILIT_PATTERN);
List<String> result = new ArrayList<String>();
for (String record : records) {
result.add(record);
}
logger.info("result:: "+result.size() );
parser = new TestResultParser(result);
return log(parser,file );
}
private boolean log(TestResultParser parser,File file) {
boolean logFlag = false;
if(parser != null) {
TestResultMessage report = parser.getTestResultMessage();
//TestResultModel model = parser.getTestResultModel();
List<Result> list = parser.getTestResultModelList();
logger.info("list:: "+ list.size());
logFlag = (new DBLogger()).log(list);
// for(Result model:list){
// if(model != null && model.getTestCode() != null && !model.getTestCode().equals("")) {
// (new DBLogger()).log(model);
//
// }
// }
if(report != null) {
if (params.isOutputOnFile()) {
(new FileLogger()).log(report,file.getName());
}
}
}
return logFlag;
}