How to create a temporary directory/folder in Java?
If you are using JDK 7 use the new Files.createTempDirectory class to create the temporary directory.
Path tempDirWithPrefix = Files.createTempDirectory(prefix);
Before JDK 7 this should do it:
public static File createTempDirectory()
throws IOException
{
final File temp;
temp = File.createTempFile("temp", Long.toString(System.nanoTime()));
if(!(temp.delete()))
{
throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
}
if(!(temp.mkdir()))
{
throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
}
return (temp);
}
You could make better exceptions (subclass IOException) if you want.
create a temporary file with a specified name in java
You can directly give the location and file name or You can access local filesystem and find the temp directory
String tempDir=System.getProperty("java.io.tmpdir");
you can use temp directory and your custom file name.
public static void main(String[] args) {
try {
String tempDir=System.getProperty("java.io.tmpdir");
String sCourrier ="sahu";
File file = new File(tempDir+"newfile.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(sCourrier.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
Creating temp Folder in java
You are almost done with create tempfolder, see this:
import java.io.File;
import java.io.IOException;
public class TempFolder {
public static void main(String[] args) throws IOException {
File file = File.createTempFile("my_prefix", "");
System.out.println(file.getAbsolutePath() + " isFile: " + file.isFile() + " isDir:" + file.isDirectory());
file.delete();
file.mkdir();
System.out.println(file.getAbsolutePath() + " isFile: " + file.isFile() + " isDir:" + file.isDirectory());
}
}
first createTempFile will make a real file for you, just remove it and make a directory using the same name.
I use osx, too. My result is:
/var/folders/aQ/aQLNlFLOF28xewK2A7i0X++++TM/-Tmp-/my_prefix8720723534029791962 isFile: true isDir:false
/var/folders/aQ/aQLNlFLOF28xewK2A7i0X++++TM/-Tmp-/my_prefix8720723534029791962 isFile: false isDir:true
how can I create a temporary folder in java 6?
I've never seen a good solution for this, but this is how I've done it.
File temp = File.createTempFile("folder-name","");
temp.delete();
temp.mkdir();
How to create temporary directory structure with files
If I understand your question right, I think using more of what the File class has to offer will help. See http://docs.oracle.com/javase/7/docs/api/java/io/File.html
I think you would have a more typical approach if you tried something like this:
File tempfldr = new File("C:\\rootFolder\\childFolder1");
tempfldr.mkdirs();
File tempfldr2 = new File("C:\\rootFolder\\childFolder2");
tempfldr2.mkdirs();
File child1 = File.createTempFile("prefix_val", "suffix_val", tempfldr);
File child2 = File.createTempFile("prefix_val", "suffix_val", tempfldr2);
How to create temporary folder with zip folder inside
Using zip4j you can do something like this -
ZipFile zipFile = new ZipFile(source);
ZipParameters parameters = new ZipParameters();
zipFile.addFolder(dirPath);
Here source
is the Path of your zip file that you said already exists.
How to copy the zip file to this destination. you can do in many ways..Simplest is --
org.apache.commons.io.FileUtils.copyFile(File, File)
FileUtils.copyFile(new File("/sourcefolder/some.zip"),
new File("/destination/some.zip"))
Environment variable to control java.io.tmpdir?
Hmmm -- since this is handled by the JVM, I delved into the OpenJDK VM source code a little bit, thinking that maybe what's done by OpenJDK mimics what's done by Java 6 and prior. It isn't reassuring that there's a way to do this other than on Windows.
On Windows, OpenJDK's get_temp_directory()
function makes a Win32 API call to GetTempPath()
; this is how on Windows, Java reflects the value of the TMP
environment variable.
On Linux and Solaris, the same get_temp_directory()
functions return a static value of /tmp/
.
I don't know if the actual JDK6 follows these exact conventions, but by the behavior on each of the listed platforms, it seems like they do.
Related Topics
How to Use a Delimiter with Scanner.Usedelimiter in Java
Strange Floating-Point Behaviour in a Java Program
Any Way to Invoke a Private Method
How to Inject a Property Value into a Spring Bean Which Was Configured Using Annotations
Java Thread Garbage Collected or Not
What's the Best Way to Distribute Java Applications
Is There an Executorservice That Uses the Current Thread
Why Doesn't Java Support Unsigned Ints
How to Create a Jandex Index in Quarkus for Classes in a External Module
Remove All Occurrences of Char from String
How to Create a Temporary Directory/Folder in Java
Javafx: Location Is Not Set Error
Call an Executable and Pass Parameters
Handling Exceptions from Java Executorservice Tasks
Sort a Two Dimensional Array Based on One Column
No @Xmlrootelement Generated by Jaxb
Difference Between Paint, Paintcomponent and Paintcomponents in Swing