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

25
LINES

< > BotCompany Repo | #1001113 - saveBinaryFile

JavaX fragment (include)

/** writes safely (to temp file, then rename) */
public static byte[] saveBinaryFile(String fileName, byte[] contents) ctex {
  File file = new File(fileName);
  File parentFile = file.getParentFile();
  if (parentFile != null)
    parentFile.mkdirs();
  String tempFileName = fileName + "_temp";
  FileOutputStream fileOutputStream = newFileOutputStream(tempFileName);
  fileOutputStream.write(contents);
  fileOutputStream.close();
  if (file.exists() && !file.delete())
    throw new IOException("Can't delete " + fileName);

  if (!new File(tempFileName).renameTo(file))
    throw new IOException("Can't rename " + tempFileName + " to " + fileName);
    
  ifndef LeanMode
  vmBus_send('wroteFile, file);
  endifndef
  ret contents;
}

static byte[] saveBinaryFile(File fileName, byte[] contents) {
  ret saveBinaryFile(fileName.getPath(), contents);
}

Author comment

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/4
Text MD5: ef7921b1a407e660a2c19038a7ed9202
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-27 15:00:49
Source code size: 880 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 532 / 3237
Version history: 3 change(s)
Referenced in: [show references]