I need to create a temp file, so I tried this:
String[] TempFiles = {"c1234c10","c1234c11","c1234c12","c1234c13"}; for (int i = 0; i <= 3; i++) { try { String tempFile = TempFiles[i]; File temp = File.createTempFile(tempFile, ".xls"); System.out.println("Temp file : " + temp.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } }
The output is something like this:
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c108415816200650069233.xls Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c113748833645638701089.xls Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c126104766829220422260.xls Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c137493179265536640669.xls
Now, I don't want the extra numbers (long int) which is getting added to the file name. How can I achieve that? Thanks
Just check the return value of temp. createNewFile() . Read the specification of createNewFile() . The important word is atomic.
tmpdir") to get the default temporary file location. For Windows, the default temporary folder is %USER%\AppData\Local\Temp. For Linux, the default temporary folder is /tmp.
First, use the following snippet to get the system's temp directory:
String tDir = System.getProperty("java.io.tmpdir");
Then use the tDir
variable in conjunction with your tempFiles[]
array to create each file individually.
Using Guava:
import com.google.common.io.Files; ... File myTempFile = new File(Files.createTempDir(), "MySpecificName.png");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With