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

39
LINES

< > BotCompany Repo | #1029429 // saveTextFile_rawBytes

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

Libraryless. Click here for Pure Java version (2737L/18K).

1  
static File saveTextFile_rawBytes(S fileName, S contents) ctex {
2  
  CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
3  
  try {
4  
    File file = new File(fileName);
5  
    mkdirsForFile(file);
6  
    String tempFileName = fileName + "_temp";
7  
    File tempFile = new File(tempFileName);
8  
    if (contents != null) {
9  
      if (tempFile.exists()) try {
10  
        S saveName = tempFileName + ".saved." + now();
11  
        copyFile(tempFile, new File(saveName));
12  
      } catch (Throwable e) { printStackTrace(e); }
13  
      FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
14  
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "ISO-8859-1");
15  
      PrintWriter printWriter = new PrintWriter(outputStreamWriter);
16  
      printWriter.print(contents);
17  
      printWriter.close();
18  
    }
19  
    
20  
    if (file.exists() && !file.delete())
21  
      throw new IOException("Can't delete " + fileName);
22  
  
23  
    if (contents != null)
24  
      if (!tempFile.renameTo(file))
25  
        throw new IOException("Can't rename " + tempFile + " to " + file);
26  
        
27  
    ifndef LeanMode
28  
    vmBus_send('wroteFile, file);
29  
    endifndef
30  
    ret file;
31  
  } finally {
32  
    action.done();
33  
  }
34  
}
35  
36  
static File saveTextFile_rawBytes(File fileName, S contents) {
37  
  saveTextFile_rawBytes(fileName.getPath(), contents);
38  
  ret fileName;
39  
}

Author comment

Began life as a copy of #1001042

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029429
Snippet name: saveTextFile_rawBytes
Eternal ID of this version: #1029429/1
Text MD5: 0537a31b77ffe3c610d3af3f2d593007
Transpilation MD5: 7d31c933a78f49d9803e361d47ed0467
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-05 22:30:22
Source code size: 1418 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 163 / 246
Referenced in: [show references]