com.day.io.file.TempDirectory Maven / Gradle / Ivy
/**
* $Id: TempDirectory.java 12345 2004-08-22 04:56:09Z fielding $
*
* Copyright 1997-2004 Day Management AG
* Barfuesserplatz 6, 4001 Basel, Switzerland
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Day Management AG, ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Day.
*
* @version $Revision: 1.7 $, $Date: 2004-08-22 06:56:09 +0200 (Sun, 22 Aug 2004) $
*/
package com.day.io.file;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Encapsulates a temporary directory, that should work cross-platform
* and on JDK 1.1. This is mostly useful for test code.
*
* Note the finalize method, which should delete the directory when
* this object is garbage collected.
*/
public class TempDirectory {
private static final Logger log =
LoggerFactory.getLogger(TempDirectory.class);
private File tempDir = null;
public TempDirectory(String name) throws IOException {
createTempDir(name);
}
private void createTempDir(String name) throws IOException {
log.debug("Starting createTempDir ("+""+"name : "+name + " , "+")");
tempDir = new File(System.getProperty("java.io.tmpdir"), "tmp"+name);
int i=0;
while (tempDir.exists()) {
i++;
tempDir = new File(System.getProperty("java.io.tmpdir"), "tmp"+name+Integer.toString(i));
}
// if (tempDir.exists()) {
// throw new IOException("Could not make temp directory "+tempDir.getPath()+" - it already exists");
// }
if (!tempDir.mkdir()) { throw new IOException("Could not make temp directory "+tempDir.getPath()); }
empty();
}
/**
* @todo This should be recursive
*/
public void empty() throws IOException {
log.debug("Starting empty");
File[] files = listFiles();
for (int i=0; i