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 384 views
1 answer 275 views
1 answer 299 views
1 answer 292 views
292 views asked Jul 21, 2016 by avibootz
1 answer 293 views
1 answer 242 views
...