tempfiles.java

来自「联合国农粮署牵头开发的geonetwork源代码最新版」· Java 代码 · 共 86 行

JAVA
86
字号
/** * TempFiles.java * * @author Created by Omnicore CodeGuide */package org.wfp.vam.intermap.kernel;import java.io.*;import java.util.*;public abstract class TempFiles{//	private static File dir;	private int minutes;	private Timer timer;	/**	 * Periodically starts a process that cleans up the temporary files	 * every n minutes	 *	 */	protected TempFiles(int minutes) throws Exception	{//		dir = new File(tempDir);//		if (!dir.isDirectory())//			throw new Exception("Invalid temp directory '"+tempDir+"'");		timer = new Timer();		timer.schedule(new RemindTask(),					   0,					   minutes * 60 * 1000);	}	public void end()	{		timer.cancel();	}	abstract public File getDir();//	{//		return dir;//	}	/**	 * Creates a temporary File	 *	 * @return   a tempoarary File	 *	 * @throws   If a file could not be created	 *	 */	public File getFile() throws IOException	{		return getFile(".tmp");	}	public File getFile(String extension) throws IOException	{		if( ! extension.startsWith("."))			extension = "."+extension;		File tf = File.createTempFile("temp", extension, getDir());		tf.deleteOnExit();		return tf;	}	// Delete all the files in the temp directory	class RemindTask extends TimerTask {		public void run()		{			for (File f: getDir().listFiles())			{				Calendar last = Calendar.getInstance();				last.add(Calendar.MINUTE, -minutes);				// Only files whose name start with ".temp" are deleted				if (f.getName().startsWith("temp") && last.getTime().after( new Date(f.lastModified())) )					f.delete();			}		}	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?