tempdirectorymanager.java

来自「MyDownloader 是一款使用 http 协议(RFC 1867)用于下载」· Java 代码 · 共 66 行

JAVA
66
字号
/*
 * Copyright 2007 JavaAtWork All rights reserved.
 * Use is subject to license terms.
 */
package com.javaatwork.mydownloader.utils;

import java.io.File;

/**
 * Class that is responsible for creatin a temp directory
 * and deleting the files and in this directory.
 * 
 * @author Johannes Postma
 */
public class TempDirectoryManager {

	private static File tempDirectory = null;
	
	/**
	 * Creates the temp directory.
	 * 
	 * @throws Exception If the temp directory cannot be created.
	 */
	public static void createTempDirectory() throws Exception {
		
		File dir = new File(System.getProperty("java.io.tmpdir"));
		tempDirectory = new File(dir, "mydownloader-" + System.currentTimeMillis());
		
		if (tempDirectory.exists()) {
			return;
		}
		
		boolean created = tempDirectory.mkdir();
		
		if (created == false) {
			throw new Exception ("Temp directory could not be created");
		}
	}
	
	
	/**
	 * Deletes the temp directory.
	 */
	public static void deleteTempDirectory() {
		
		// deletes all the files in this directory
		File [] files = tempDirectory.listFiles();
		
		for (int i = 0; i < files.length; i++) {
			files[i].delete();
		}
		
		tempDirectory.delete();
	}
	
	
	/**
	 * Returns the temp directory.
	 * 
	 * @return The temp directory.
	 */
	public static File getTempDirectory() {
		return tempDirectory;
	}	
}

⌨️ 快捷键说明

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