Android examples for Hardware:Directory
Creates an empty temporary file in the given directory using the given prefix and suffix as part of the file name.
import android.content.ContentResolver; import android.content.Context; import android.net.Uri; import android.provider.CalendarContract; import com.android.calendar.CalendarEventModel; import java.io.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Random; import java.util.TimeZone; public class Main{ private static final Random tempFileRandom = new Random(); /**//from ww w . ja v a 2s. c o m * Creates an empty temporary file in the given directory using the given * prefix and suffix as part of the file name. If {@code suffix} is null, {@code .tmp} is used. * * <p>Note that this method does <i>not</i> call {@link #deleteOnExit}, but see the * documentation for that method before you call it manually. * * @param prefix * the prefix to the temp file name. * @param suffix * the suffix to the temp file name. * @param directory * the location to which the temp file is to be written, or * {@code null} for the default location for temporary files, * which is taken from the "java.io.tmpdir" system property. It * may be necessary to set this property to an existing, writable * directory for this method to work properly. * @return the temporary file. * @throws IllegalArgumentException * if the length of {@code prefix} is less than 3. * @throws IOException * if an error occurs when writing the file. */ public static File createTempFile(String prefix, String suffix, File directory) throws IOException { // Force a prefix null check first if (prefix.length() < 3) { throw new IllegalArgumentException( "prefix must be at least 3 characters"); } if (suffix == null) { suffix = ".tmp"; } File tmpDirFile = directory; if (tmpDirFile == null) { String tmpDir = System.getProperty("java.io.tmpdir", "."); tmpDirFile = new File(tmpDir); } File result; do { result = new File(tmpDirFile, prefix + tempFileRandom.nextInt(Integer.MAX_VALUE) + suffix); } while (!result.createNewFile()); return result; } }