Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { /** * Delete the earliest file in the specified directory * * @param dir * The specified directory * @param exceptFile * Exclude the file name */ public static final void deleteEarliestFile(File dir, String exceptFile) { if (dir != null && dir.isDirectory()) { File earlyFile = null; File[] files = dir.listFiles(); if (files.length == 0) return; for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.getName().equals(exceptFile)) continue; if (earlyFile == null) { earlyFile = files[i]; continue; } if (earlyFile.lastModified() > f.lastModified()) { earlyFile = f; } } if (earlyFile != null) earlyFile.delete(); } } }