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
46 lines (36 loc) · 1.49 KB
/
Copy pathMain.java
File metadata and controls
46 lines (36 loc) · 1.49 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
37
38
39
40
41
42
43
44
45
46
package modern.challenge;
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 {
final Path tmpFile = Files.createTempFile(
customBaseDir, customFilePrefix, customFilesuffix);
System.out.println("Created as: " + tmpFile);
System.out.println("Sleep for 10 seconds until deletion ...");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Deleting the temporary file ...");
try {
Files.delete(tmpFile);
} catch (IOException e) {
// handle exception, don't print it as below
System.err.println(e);
}
System.out.println("Shutdown hook completed...");
System.out.println("Delete file completed...");
}
});
//simulate some operations with temp file until delete it
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
System.err.println(e);
}
}
}