日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java 临时文件_在Java中使用临时文件/文件夹

發(fā)布時(shí)間:2023/12/16 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 临时文件_在Java中使用临时文件/文件夹 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)。接下來(lái),我們將學(xué)習(xí)如何創(chuàng)建一個(gè)臨時(shí)文件夾/文件。

創(chuàng)建一個(gè)臨時(shí)文件夾/文件

創(chuàng)建臨時(shí)文件夾可以使用以下方法完成:

· Path createTempDirectory (Path dir, String prefix, FileAttribute>... attrs)

這是類中的一種 static方法 Files ,可以按如下方式使用:

· 讓我們?cè)诓僮飨到y(tǒng)的默認(rèn)位置創(chuàng)建一個(gè)沒(méi)有前綴的臨時(shí)文件夾:

Java

1

// C:\Users\Anghel\AppData\Local\Temp\8083202661590940905

2

Path tmpNoPrefix = Files.createTempDirectory(null);

讓我們?cè)诓僮飨到y(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);

讓我們?cè)趲в凶远x前綴的自定義位置中創(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í)文件可以通過(guò)以下方式完成:

· Path createTempFile (Path dir, String prefix, String suffix, FileAttribute>... attrs

這是類中的一種static方法Files ,可以按如下方式使用:

· 讓我們?cè)诓僮飨到y(tǒng)的默認(rèn)位置創(chuàng)建一個(gè)沒(méi)有前綴和后綴的臨時(shí)文件:

Java

1

// C:\Users\Anghel\AppData\Local\Temp\16106384687161465188.tmp

2

Path tmpNoPrefixSuffix = Files.createTempFile(null, null);

讓我們?cè)诓僮飨到y(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);

讓我們?cè)趲в凶远x前綴和后綴的自定義位置中創(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);

接下來(lái),我們將研究刪除臨時(shí)文件夾/文件的不同方法。

通過(guò)關(guān)機(jī)掛鉤刪除臨時(shí)文件夾/文件

刪除臨時(shí)文件夾/文件是可以由操作系統(tǒng)或?qū)S霉ぞ咄瓿傻娜蝿?wù)。但是,有時(shí),我們需要以編程方式進(jìn)行控制,并基于不同的設(shè)計(jì)考慮因素刪除文件夾/文件。

該問(wèn)題的解決方案依賴于可通過(guò)該方法實(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

}

通過(guò)deleteOnExit()刪除臨時(shí)文件夾/文件

刪除臨時(shí)文件夾/文件的另一種解決方案依賴于該 File.deleteOnExit()方法。通過(guò)調(diào)用此方法,我們可以注冊(cè)刪除文件夾/文件。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

}

通過(guò)DELETE_ON_CLOSE刪除臨時(shí)文件

刪除臨時(shí)文件所依賴的另一個(gè)解決方案 StandardOpenOption.DELETE_ON_CLOSE (在關(guān)閉流時(shí)刪除該文件)。例如,下面的代碼段通過(guò)createTempFile() 方法創(chuàng)建一個(gè)臨時(shí)文件,并為該文件打開(kāi)一個(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

}

最后,開(kāi)發(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中使用临时文件/文件夹的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。