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).

static File saveTextFile_rawBytes(S fileName, S contents) ctex {
  CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
  try {
    File file = new File(fileName);
    mkdirsForFile(file);
    String tempFileName = fileName + "_temp";
    File tempFile = new File(tempFileName);
    if (contents != null) {
      if (tempFile.exists()) try {
        S saveName = tempFileName + ".saved." + now();
        copyFile(tempFile, new File(saveName));
      } catch (Throwable e) { printStackTrace(e); }
      FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "ISO-8859-1");
      PrintWriter printWriter = new PrintWriter(outputStreamWriter);
      printWriter.print(contents);
      printWriter.close();
    }
    
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);
  
    if (contents != null)
      if (!tempFile.renameTo(file))
        throw new IOException("Can't rename " + tempFile + " to " + file);
        
    ifndef LeanMode
    vmBus_send('wroteFile, file);
    endifndef
    ret file;
  } finally {
    action.done();
  }
}

static File saveTextFile_rawBytes(File fileName, S contents) {
  saveTextFile_rawBytes(fileName.getPath(), contents);
  ret fileName;
}

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: 164 / 247
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)