How to create temporary file in Java

2 Answers

0 votes
import java.io.File;
import java.io.IOException;
 
public class MyClass {
 	public static void main(String[] args) {
		try {
			File tmpFile = File.createTempFile("data", null);
			
			System.out.println(tmpFile.getCanonicalPath());
			
			tmpFile.deleteOnExit();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
}




/*
run:

/tmp/data11515482759801971528.tmp

*/

 



answered Dec 1, 2023 by avibootz
0 votes
import java.io.File;
import java.io.IOException;
 
public class MyClass {
 	public static void main(String[] args) {
		try {
			File tmpFile = File.createTempFile("text", ".temp", new File("/tmp"));
			
			System.out.println(tmpFile.getCanonicalPath());
			
			tmpFile.deleteOnExit();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
}




/*
run:

/tmp/text3382029218984029179.temp

*/

 



answered Dec 1, 2023 by avibootz

Related questions

2 answers 266 views
1 answer 174 views
1 answer 207 views
1 answer 213 views
213 views asked Jul 21, 2016 by avibootz
1 answer 204 views
1 answer 153 views
...