Here you can find the source of createTemp(File file)
Parameter | Description |
---|---|
IllegalArgumentException | if file == null |
SecurityException | if a security manager exists and denies read access to the file |
RuntimeException | (or some subclass) if any other error occurs; this may merely wrap some other underlying Throwable |
public static File createTemp(File file) throws IllegalArgumentException, SecurityException, RuntimeException
/*//from w ww . jav a2s. c o m Copyright ? 2008 Brent Boyer This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project). If not, see <http://www.gnu.org/licenses/>. */ import bb.science.FormatUtil; import bb.util.Check; import bb.util.StringUtil; import bb.util.ThrowableUtil; import bb.util.logging.LogUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.util.Random; import java.util.logging.Level; import org.junit.Assert; import org.junit.Test; public class Main{ /** * Returns <code>{@link #createTemp(File) createTemp}( new File(path) )</code>. * <p> * @return a new File constructed from path * @throws IllegalArgumentException if path == null * @throws SecurityException if a security manager exists and denies read access to the file * @throws RuntimeException (or some subclass) if any other error occurs; this may merely wrap some other underlying Throwable */ public static File createTemp(String path) throws IllegalArgumentException, SecurityException, RuntimeException { Check.arg().notNull(path); return createTemp(new File(path)); } /** * Creates a temporary normal file. * <p> * Does the following actions: * <ol> * <li>attempts to delete file if it currently exists</li> * <li> * attempts to create a new empty version of file * (<b>Warning:</b> does not create any parent directories that do not currently exist, * because those directories cannot be automatically deleted upon exit. * Therefore, this method fails if the parent directories do not all exist.) * </li> * <li>calls deleteOnExit on file, ensuring that file is temporary</li> * </ol> * Note that unlike {@link File#createTempFile File.createTempFile}, * which adds random numbers inside the file names that it creates, * this method does no filename change, which allows you to work with files of a known name. * Furthermore, this method catches all checked Exceptions and converts them to a RuntimeException before rethrowing them, * so it may be used to assign fields. * <p> * @return the supplied file arg * @throws IllegalArgumentException if file == null * @throws SecurityException if a security manager exists and denies read access to the file * @throws RuntimeException (or some subclass) if any other error occurs; this may merely wrap some other underlying Throwable * @see <a href="http://www.devx.com/Java/Article/22018/1954?pf=true">Bugs in temp file deletion</a> */ public static File createTemp(File file) throws IllegalArgumentException, SecurityException, RuntimeException { try { Check.arg().notNull(file); if (file.exists()) { boolean deleted = file.delete(); if (!deleted) throw new RuntimeException( "file = " + file.getPath() + " was previously existing and failed to be deleted"); } if (file.getParentFile() != null) { // must check, since is null if file is directly in the root directory if (!file.getParentFile().exists()) throw new IllegalStateException( "file.getParentFile() = " + file.getParentFile().getPath() + " does not currently exist"); } boolean created = file.createNewFile(); if (!created) throw new RuntimeException("file = " + file.getPath() + " failed to be created"); file.deleteOnExit(); // +++ warning: see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171239 return file; } catch (Throwable t) { throw ThrowableUtil.toRuntimeException(t); } } /** * Trys to delete file, and throws an IOException if the delete failed. * It is useful because {@link File#delete File.delete} unfortunately merely returns a boolean indicating * the success of the operation, rather than throwing an Exception. * <p> * Note: {@link DirUtil#delete DirUtil.delete} should be used to delete directories. * <p> * @param file a normal file that is to be deleted * @throws IllegalArgumentException if file is {@link Check#validFile not valid} * @throws SecurityException if a security manager exists and denies read access to the file * @throws IOException if an I/O problem occurs; this includes failure of the file to be deleted */ public static void delete(File file) throws IllegalArgumentException, SecurityException, IOException { Check.arg().validFile(file); boolean deleted = file.delete(); if (!deleted) throw new IOException("Failed to delete " + file.getPath()); } }