In the temporary path generation file server, deleted after 7 days

The project is to call an external interface, push data. One interface is a batch csv file transfer data. This is what we get here to the List <Entity> data, and then converted to a csv file to the interface inside. Csv file is generated to relate to want to write to a file server. I asked about the suggestion to write the temporary file path. There are not generated files after a successful call to immediately delete, delete the recommended seven days.

So in 2 hours before the start of line items on the changed code.

The idea is to delete the file: Find files seven days before all of the path, and then delete it.

Here we must make the file time stamps. Add file when naming the date For example: exaple_file_20190620_12312.csv

Find a way to find files beginning exaple_file_20190613, then delete it.

Record the code below:

Acquisition path

String courseFile=System.getProperty("java.io.tmpdir");
log.info("path:" + courseFile);
fileDelete(courseFile);

Delete the path

public static void fileDelete(String filePath) throws Exception{
		File tempDir = new File(filePath);
		File[] subFiles = tempDir.listFiles();
		String prefix = "example_" + DateUtils
				.formatDate(com.company.messag**.***.util.DateUtils.addDays(new Date(), -7), "yyyyMMdd");
		for (File file : subFiles) {
			if (file.isFile() && file.getName().startsWith(prefix)) {
				file.delete();
			}
		}
	}

Well, that's all.

Code reference this blog: https://blog.csdn.net/Leo_01169/article/details/84942958    to express my gratitude

Guess you like

Origin blog.csdn.net/luo_yu_1106/article/details/92985259