Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

28
LINES

< > BotCompany Repo | #1005539 // saveGZTextFile

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2576L/17K).

1  
static void saveGZTextFile(File file, S contents, S encoding default "UTF-8") ctex {
2  
  File parentFile = file.getParentFile();
3  
  if (parentFile != null)
4  
    parentFile.mkdirs();
5  
  String tempFileName = file.getPath() + "_temp";
6  
  File tempFile = new File(tempFileName);
7  
  if (contents != null) {
8  
    if (tempFile.exists()) try {
9  
      S saveName = tempFileName + ".saved." + now();
10  
      copyFile(tempFile, new File(saveName));
11  
    } catch (Throwable e) { printStackTrace(e); }
12  
    FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
13  
    GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
14  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(gos, encoding);
15  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
16  
    printWriter.print(contents);
17  
    printWriter.close();
18  
    gos.close();
19  
    fileOutputStream.close();
20  
  }
21  
  
22  
  if (file.exists() && !file.delete())
23  
    throw new IOException("Can't delete " + file.getPath());
24  
25  
  if (contents != null)
26  
    if (!tempFile.renameTo(file))
27  
      throw new IOException("Can't rename " + tempFile + " to " + file);
28  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1005539
Snippet name: saveGZTextFile
Eternal ID of this version: #1005539/2
Text MD5: 22412686199f51e969bb9f29617e893b
Transpilation MD5: d2258cccce1db74a709fcd0808806135
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-27 23:28:09
Source code size: 1148 bytes / 28 lines
Pitched / IR pitched: No / No
Views / Downloads: 597 / 653
Version history: 1 change(s)
Referenced in: [show references]