Libraryless. Click here for Pure Java version (9250L/51K).
1 | /** writes safely (to temp file, then rename) */ |
2 | public static byte[] saveBinaryFile(String fileName, byte[] contents) ctex { |
3 | File file = new File(fileName); |
4 | File parentFile = file.getParentFile(); |
5 | if (parentFile != null) |
6 | parentFile.mkdirs(); |
7 | String tempFileName = fileName + "_temp"; |
8 | try (FileOutputStream fileOutputStream = newFileOutputStream(tempFileName)) { |
9 | fileOutputStream.write(contents); |
10 | } |
11 | if (file.exists() && !file.delete()) |
12 | throw new IOException("Can't delete " + fileName); |
13 | |
14 | if (!new File(tempFileName).renameTo(file)) |
15 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
16 | |
17 | ifndef LeanMode |
18 | vmBus_send('wroteFile, file); |
19 | endifndef |
20 | ret contents; |
21 | } |
22 | |
23 | static byte[] saveBinaryFile(File fileName, byte[] contents) { |
24 | ret saveBinaryFile(fileName.getPath(), contents); |
25 | } |
Began life as a copy of #2000464
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1001113 |
Snippet name: | saveBinaryFile |
Eternal ID of this version: | #1001113/5 |
Text MD5: | 01937b87a511127498dc8f4d902ba013 |
Transpilation MD5: | 6e177032ef863336057736f8ff3180cc |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-08-07 02:47:16 |
Source code size: | 865 bytes / 25 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 755 / 3546 |
Version history: | 4 change(s) |
Referenced in: | [show references] |