package com.perforce.jbrown.ctf; import java.io.File; import java.io.IOException; /*** * Creates a temp file in the same directory as the provided file. * pass the special string "null" to use the temp dir. * @author jbrown * */ public class CreateTempFile { public static void main(String[] args) { // TODO Auto-generated method stub if (args.length < 1) { System.out.println("Please provide the name of an existing file."); System.exit(1); } String existsFile = args[0]; File file = null ; File tmpDir = null ; String tmpDirPath = null ; if ( ! "null".equalsIgnoreCase(existsFile)) { try { file = new File(args[0]); if (!file.exists()) { System.out.println("File '" + existsFile + "' does not exist. Please provide the name of an existing file."); System.exit(2); } tmpDir = file.getParentFile(); tmpDirPath = tmpDir.getPath(); } catch (Exception e) { e.printStackTrace(System.out); System.exit(3); } } try { String tempFileName = createTempFileName(tmpDirPath); System.out.println("Temp file successfully created: " + tempFileName + " (will be deleted on exit)"); } catch (IOException e) { e.printStackTrace(System.out); System.exit(4); } } public static String createTempFileName(String tmpDirName) throws IOException { File tmpDir = null; if (tmpDirName != null ) { tmpDir = new File(tmpDirName); } System.out.println("Creating file in " + tmpDir); File tmpFile = File.createTempFile(TMP_FILE_PFX, TMP_FILE_SFX, tmpDir); tmpFile.deleteOnExit(); return tmpFile.getPath(); } public static final String TMP_FILE_PFX = "p4j"; public static final String TMP_FILE_SFX = ".tmp"; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 19991 | Joel Brown |
Example to create a Temp File. P4Java does not throw an exception when a temp file for a sync cannot be created. |