Skip to content

Instantly share code, notes, and snippets.

@hackjutsu
Last active June 7, 2020 00:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hackjutsu/ced00e69b18ce9f0af124dea9ece586d to your computer and use it in GitHub Desktop.
Save hackjutsu/ced00e69b18ce9f0af124dea9ece586d to your computer and use it in GitHub Desktop.
Use java.io.tmpdir to get system tmp directory.
import java.io.File;
import java.io.IOException;
public class TmpDirExample {
public static void main(String[] args) {
String tmpdir = System.getProperty("java.io.tmpdir");
System.out.println("The default value of the java.io.tmpdir system property is: \""
+ tmpdir + "\"\n");
//Specify some temporary files.
String prefix = "file";
String suffix = ".txt";
File tempFile = null;
File tempFile2 = null;
File tempFile3 = null;
File directory = new File("/home/stathis");
try {
//Create two temporary files.
tempFile = File.createTempFile(prefix, suffix);
tempFile2 = File.createTempFile(prefix, null);
tempFile3 = File.createTempFile(prefix, suffix, directory);
}
catch (IOException ex) {
System.err.println("An IOException was caught: " + ex.getMessage());
ex.printStackTrace();
}
//Printing the name of every file.
System.out.println("A new file called \"" + tempFile.getName()
+ "\" was created in the directory: \"" + tmpdir + "\"");
System.out.println("A new file called \"" + tempFile2.getName()
+ "\" was created in the directory: \"" + tmpdir + "\"\n");
System.out.println("A new file called \"" + tempFile3.getName()
+ "\" was created in the directory: \"" + directory.getName() + "\"\n");
//Printing the parent directories of every file.
System.out.println("The parent directory of the file \"" + tempFile.getName()
+ "\" is: \"" + tempFile.getParent() + "\"");
System.out.println("The parent directory of the file \"" + tempFile2.getName()
+ "\" is: \"" + tempFile2.getParent() + "\"");
System.out.println("The parent directory of the file \"" + tempFile3.getName()
+ "\" is: \"" + tempFile3.getParent() + "\"\n");
//Delete the temporary files.
if(tempFile.delete())
System.out.println("Successfully deleted the file with name: \""
+ tempFile.getName() + "\"");
else
System.out.println("Couldn't delete the file with name: \"" + tmpdir + "\"");
if(tempFile2.delete())
System.out.println("Successfully deleted the file with name: \""
+ tempFile2.getName() + "\"");
else
System.out.println("Couldn't delete the file with name: \"" + tmpdir + "\"");
if(tempFile3.delete())
System.out.println("Successfully deleted the file with name: \""
+ tempFile3.getName() + "\"");
else
System.out.println("Couldn't delete the file with name: \"" + tmpdir + "\"");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment