一个关于遍历文件夹下的文件内容,遍历Map的key和value的例子


public class FindErrors {

public static void main(String[] args) {
System.out.println("Jobs Started!");

// Searching loop log directory
String filename = "c:\wolfeTest\log\";
File logDir = new File(filename);
File logList[] = logDir.listFiles();

Map<String, String> errorsMSG = new HashMap<String, String>();
// Loop and read each file
String line = "";
String str_errors = "";
int site = -1;
for (int i = 0; i < logList.length; i++) {

try {
BufferedReader br = new BufferedReader(new FileReader(filename + logList[i].getName().toString()));

while ((line = br.readLine()) != null) {
site = line.indexOf("Rows not loaded due to data errors");
if (site != -1) {
str_errors = line.substring(0, site).trim();
// If there any error, put in the map
if(!str_errors.equals("0"))
errorsMSG.put(logList[i].getName().toString(), str_errors);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

// Loop the map(errorsMSG)
Set set = errorsMSG.keySet();
for (Iterator iter = set.iterator(); iter.hasNext();) {
String key = (String) iter.next();
String value = (String) errorsMSG.get(key);
System.out.println("fileName: " + key + ", Errors: " + value);
}

System.out.println("Jobs Finished!");
}
}

原文地址:https://www.cnblogs.com/wolfe/p/3161395.html