java 循环日期 按日期递归读取文件夹中的文件,输出到txt文档

public Map editSmsOrder(ComSvcCaller svcCaller) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

Map map = new HashMap();

String start = "20140101";

String path = "E:/Control/smsemail/程序脚本";

String str = "";

try {

FileWriter fileWriter = new FileWriter(

"C:/Users/Administrator/Desktop/list.sql");

Date startDate = null;

try {

startDate = sdf.parse(start);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Date endDate = new Date();

Calendar ca = Calendar.getInstance();

Date curDate = startDate;

while (curDate.compareTo(endDate) <= 0) {

ca.setTime(curDate);

str = sdf.format(curDate);

int fileNum = 0, folderNum = 0;

File file = new File(path);

if (file.exists()) {

LinkedList<File> list = new LinkedList<File>();

File[] files = file.listFiles();

for (File file2 : files) {

if (file2.isDirectory()) {

if (file2.getAbsolutePath().contains(str)) {

fileWriter.write("@" + file2.getAbsolutePath()

+ "\r\n");

}

list.add(file2);

fileNum++;

} else {

if (file2.getAbsolutePath().contains(str)) {

fileWriter.write("@" + file2.getAbsolutePath()

+ "\r\n");

}

folderNum++;

}

}

File temp_file;

while (!list.isEmpty()) {

temp_file = list.removeFirst();

files = temp_file.listFiles();

for (File file2 : files) {

if (file2.isDirectory()) {

if (file2.getAbsolutePath().contains(str)) {

fileWriter.write("@"

+ file2.getAbsolutePath() + "\r\n");

}

list.add(file2);

fileNum++;

} else {

if (file2.getAbsolutePath().contains(str)) {

if (file2.getAbsolutePath().contains(str)) {

fileWriter.write("@"

+ file2.getAbsolutePath()

+ "\r\n");

}

folderNum++;

}

}

}

}

} else {

System.out.println("文件不存在!");

}

System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);

ca.add(ca.DATE, 1);

curDate = ca.getTime();

}

fileWriter.flush();

fileWriter.close();

} catch (IOException e1) {

}

return map;

}

 

posted @ 2017-04-27 15:15  一z都在  阅读(1079)  评论(0)    收藏  举报