java 临时文件_在Java中使用临时文件/文件夹
Java NIO.2 API提供了對(duì)使用臨時(shí)文件夾/文件的支持。例如,我們可以輕松地找到臨時(shí)文件夾/文件的默認(rèn)位置,如下所示:
Java
1
String defaultBaseDir = System.getProperty("java.io.tmpdir");
通常,在Windows中,默認(rèn)的臨時(shí)文件夾為 C:\Temp , %Windows%\Temp 或每個(gè)用戶所在的臨時(shí)目錄 Local Settings\Temp (此位置通常由TEMP 環(huán)境變量控制 )。
在Linux / Unix中,全局臨時(shí)目錄為 /tmp 和 /var/tmp 。前一行代碼將返回默認(rèn)位置,具體取決于操作系統(tǒng)。接下來,我們將學(xué)習(xí)如何創(chuàng)建一個(gè)臨時(shí)文件夾/文件。
創(chuàng)建一個(gè)臨時(shí)文件夾/文件
創(chuàng)建臨時(shí)文件夾可以使用以下方法完成:
· Path createTempDirectory (Path dir, String prefix, FileAttribute>... attrs)
這是類中的一種 static方法 Files ,可以按如下方式使用:
· 讓我們在操作系統(tǒng)的默認(rèn)位置創(chuàng)建一個(gè)沒有前綴的臨時(shí)文件夾:
Java
1
// C:\Users\Anghel\AppData\Local\Temp\8083202661590940905
2
Path tmpNoPrefix = Files.createTempDirectory(null);
讓我們在操作系統(tǒng)的默認(rèn)位置創(chuàng)建一個(gè)帶有自定義前綴的臨時(shí)文件夾:
Java
1
// C:\Users\Anghel\AppData\Local\Temp\logs_5825861687219258744
2
String customDirPrefix = "logs_";
3
Path tmpCustomPrefix = Files.createTempDirectory(customDirPrefix);
讓我們在帶有自定義前綴的自定義位置中創(chuàng)建一個(gè)臨時(shí)文件夾:
Java
1
// D:\tmp\logs_10153083118282372419
2
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
3
String customDirPrefix = "logs_";
4
Path tmpCustomLocationAndPrefix = Files.createTempDirectory(customBaseDir, customDirPrefix);
創(chuàng)建臨時(shí)文件可以通過以下方式完成:
· Path createTempFile (Path dir, String prefix, String suffix, FileAttribute>... attrs
這是類中的一種static方法Files ,可以按如下方式使用:
· 讓我們在操作系統(tǒng)的默認(rèn)位置創(chuàng)建一個(gè)沒有前綴和后綴的臨時(shí)文件:
Java
1
// C:\Users\Anghel\AppData\Local\Temp\16106384687161465188.tmp
2
Path tmpNoPrefixSuffix = Files.createTempFile(null, null);
讓我們在操作系統(tǒng)的默認(rèn)位置創(chuàng)建一個(gè)帶有自定義前綴和后綴的臨時(shí)文件:
Java
1
// C:\Users\Anghel\AppData\Local\Temp\log_402507375350226.txt
2
String customFilePrefix = "log_";
3
String customFileSuffix = ".txt";
4
Path tmpCustomPrefixAndSuffix = Files.createTempFile(customFilePrefix, customFileSuffix);
讓我們在帶有自定義前綴和后綴的自定義位置中創(chuàng)建一個(gè)臨時(shí)文件:
Java
1
// D:\tmp\log_13299365648984256372.txt
2
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
3
String customFilePrefix = "log_";
4
String customFileSuffix = ".txt";
5
Path tmpCustomLocationPrefixSuffix
6
= Files.createTempFile(customBaseDir, customFilePrefix, customFileSuffix);
接下來,我們將研究刪除臨時(shí)文件夾/文件的不同方法。
通過關(guān)機(jī)掛鉤刪除臨時(shí)文件夾/文件
刪除臨時(shí)文件夾/文件是可以由操作系統(tǒng)或?qū)S霉ぞ咄瓿傻娜蝿?wù)。但是,有時(shí),我們需要以編程方式進(jìn)行控制,并基于不同的設(shè)計(jì)考慮因素刪除文件夾/文件。
該問題的解決方案依賴于可通過該方法實(shí)現(xiàn)的關(guān)機(jī)掛鉤機(jī)制 Runtime.getRuntime().addShutdownHook() 。每當(dāng)我們需要在JVM關(guān)閉之前立即完成某些任務(wù)(例如,清理任務(wù))時(shí),此機(jī)制就很有用。它作為Java線程實(shí)現(xiàn),run() 當(dāng)JVM在關(guān)閉時(shí)執(zhí)行shutdown-hook時(shí),將調(diào)用其 方法。如下代碼所示:
Java
1
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
2
String customDirPrefix = "logs_";
3
String customFilePrefix = "log_";
4
String customFileSuffix = ".txt";
5
6
try {
7
Path tmpDir = Files.createTempDirectory(customBaseDir, customDirPrefix);
8
Path tmpFile1 = Files.createTempFile(tmpDir, customFilePrefix, customFileSuffix);
9
Path tmpFile2 = Files.createTempFile(tmpDir, customFilePrefix, customFileSuffix);
10
11
Runtime.getRuntime().addShutdownHook(new Thread() {
12
13
@Override
14
public void run() {
15
try (DirectoryStream ds = Files.newDirectoryStream(tmpDir)) {
16
for (Path file: ds) {
17
Files.delete(file);
18
}
19
20
Files.delete(tmpDir);
21
} catch (IOException e) {
22
...
23
}
24
}
25
});
26
27
//simulate some operations with temp file until delete it
28
Thread.sleep(10000);
29
} catch (IOException | InterruptedException e) {
30
...
31
}
通過deleteOnExit()刪除臨時(shí)文件夾/文件
刪除臨時(shí)文件夾/文件的另一種解決方案依賴于該 File.deleteOnExit()方法。通過調(diào)用此方法,我們可以注冊刪除文件夾/文件。JVM關(guān)閉時(shí),將執(zhí)行刪除操作:
Java
1
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
2
String customDirPrefix = "logs_";
3
String customFilePrefix = "log_";
4
String customFileSuffix = ".txt";
5
6
try {
7
Path tmpDir = Files.createTempDirectory(customBaseDir, customDirPrefix);
8
System.out.println("Created temp folder as: " + tmpDir);
9
Path tmpFile1 = Files.createTempFile(tmpDir, customFilePrefix, customFileSuffix);
10
Path tmpFile2 = Files.createTempFile(tmpDir, customFilePrefix, customFileSuffix);
11
12
try (DirectoryStream ds = Files.newDirectoryStream(tmpDir)) {
13
tmpDir.toFile().deleteOnExit();
14
15
for (Path file: ds) {
16
file.toFile().deleteOnExit();
17
}
18
} catch (IOException e) {
19
...
20
}
21
22
// simulate some operations with temp file until delete it
23
Thread.sleep(10000);
24
} catch (IOException | InterruptedException e) {
25
...
26
}
通過DELETE_ON_CLOSE刪除臨時(shí)文件
刪除臨時(shí)文件所依賴的另一個(gè)解決方案 StandardOpenOption.DELETE_ON_CLOSE (在關(guān)閉流時(shí)刪除該文件)。例如,下面的代碼段通過createTempFile() 方法創(chuàng)建一個(gè)臨時(shí)文件,并為該文件打開一個(gè)DELETE_ON_CLOSE 顯式指定的緩沖寫流:
Java
1
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
2
String customFilePrefix = "log_";
3
String customFileSuffix = ".txt";
4
Path tmpFile = null;
5
6
try {
7
tmpFile = Files.createTempFile(
8
customBaseDir, customFilePrefix, customFileSuffix);
9
} catch (IOException e) {
10
...
11
}
12
13
try (BufferedWriter bw = Files.newBufferedWriter(tmpFile,
14
StandardCharsets.UTF_8, StandardOpenOption.DELETE_ON_CLOSE)) {
15
16
//simulate some operations with temp file until delete it
17
Thread.sleep(10000);
18
} catch (IOException | InterruptedException e) {
19
...
20
}
最后,開發(fā)這么多年我也總結(jié)了一套學(xué)習(xí)Java的資料與面試題,如果你在技術(shù)上面想提升自己的話,可以關(guān)注我,私信發(fā)送領(lǐng)取資料或者在評(píng)論區(qū)留下自己的聯(lián)系方式,有時(shí)間記得幫我點(diǎn)下轉(zhuǎn)發(fā)讓跟多的人看到哦。
總結(jié)
以上是生活随笔為你收集整理的java 临时文件_在Java中使用临时文件/文件夹的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 递归函数的例子python卖鸭子_递归算
- 下一篇: k60的FTM模块:配置电机、编码器、舵