Transpiled version (5130L) is out of date.
1 | /** writes safely (to temp file, then rename) */ |
2 | static File saveTextFile(String fileName, String contents) throws IOException {
|
3 | /*ifdef CriticalActions |
4 | temp beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
|
5 | endifdef*/ |
6 | |
7 | File file = new File(fileName); |
8 | mkdirsForFile(file); |
9 | String tempFileName = fileName + "_temp"; |
10 | File tempFile = new File(tempFileName); |
11 | if (contents != null) {
|
12 | if (tempFile.exists()) try {
|
13 | S saveName = tempFileName + ".saved." + now(); |
14 | copyFile(tempFile, new File(saveName)); |
15 | } catch (Throwable e) { printStackTrace(e); }
|
16 | temp FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); |
17 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
18 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
19 | printWriter.print(contents); |
20 | printWriter.close(); |
21 | } |
22 | |
23 | if (file.exists() && !file.delete()) |
24 | throw new IOException("Can't delete " + fileName);
|
25 | |
26 | if (contents != null) |
27 | if (!tempFile.renameTo(file)) |
28 | throw new IOException("Can't rename " + tempFile + " to " + file);
|
29 | |
30 | ifndef LeanMode |
31 | vmBus_send('wroteFile, file);
|
32 | endifndef |
33 | ret file; |
34 | } |
35 | |
36 | static File saveTextFile(File fileName, String contents) ctex {
|
37 | saveTextFile(fileName.getPath(), contents); |
38 | ret fileName; |
39 | } |
Began life as a copy of #2000323 Can now handle "null" contents (deletes file)
download show line numbers debug dex old transpilations
Travelled to 23 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dpqxeycirhfy, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, omdjrrnzbjjv, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, skddmizpvtmy, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv, yanjaxplzisb
No comments. add comment
| Snippet ID: | #1001042 |
| Snippet name: | saveTextFile (function) |
| Eternal ID of this version: | #1001042/10 |
| Text MD5: | b59195fadef76f6178260d6dee014a1e |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-04-30 15:05:56 |
| Source code size: | 1398 bytes / 39 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1655 / 8247 |
| Version history: | 9 change(s) |
| Referenced in: | [show references] |