forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
36 lines (26 loc) · 1.07 KB
/
Copy pathMain.java
File metadata and controls
36 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package modern.challenge;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) {
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
String customFilePrefix = "log_";
String customFileSuffix = ".txt";
try {
Path tmpFile = Files.createTempFile(customBaseDir, customFilePrefix, customFileSuffix);
System.out.println("Created as: " + tmpFile);
System.out.println("Sleep for 10 seconds until deletion ...");
File asFile = tmpFile.toFile();
asFile.deleteOnExit();
// simulate some operations with temp file until delete it
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
// handle exception, don't print it as below
System.err.println(e);
}
System.out.println("Delete file completed ...");
}
}