Creates a file, making sure that its name is unique.
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
http://www.cs.umass.edu/~mccallum/mallet
This software is provided under the terms of the Common Public License,
version 1.0, as published by http://www.opensource.org. For further
information, see the file `LICENSE' included with this distribution. */
//package cc.mallet.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
*
* @author <a href="mailto:casutton@cs.umass.edu">Charles Sutton</a>
* @version $Id: ArrayUtils.java,v 1.1 2007/10/22 21:37:40 mccallum Exp $
*/
public class Util {
/**
* Creates a file, making sure that its name is unique.
* The file will be created as if by <tt>new File(dir, prefix+i+extension)</tt>,
* where i is an integer chosen such that the returned File does not exist.
* @param dir Directory to use for the returned file
* @param prefix Prefix of the file name (before the uniquifying integer)
* @param extension Suffix of the file name (after the uniquifying integer)
*/
public static File uniqueFile (File dir, String prefix, String extension)
throws IOException
{
File f = null;
int i = 0;
boolean wasCreated = false;
while (!wasCreated) {
if (dir != null) {
f = new File (dir, prefix+i+extension);
} else {
f = new File (prefix+i+extension);
}
wasCreated = f.createNewFile ();
i++;
}
return f;
}
}
Related examples in the same category