title: How to do an atomic SFTP file transfer using JSch, so that the file is not accessable until the write process has finished?
body: <p>I've written a small Java program which uses Jsch to transfer multiple textfiles to a remote server.
  Since individual files can get quite big the transfer takes up to 20 seconds.</p>
  
  <p>On the remote server the resulting file will get accessed (read) at various points of time which I have no control over. I've tested to copy the file during the SFTP transfer on the server. The copied file did not have the full data it needs to be complete since it was not fully available to the time of the copy command.</p>
  
  <p>How can I ensure that the file can get accessed only after the transfer has fully finished, so that the file can get read completely?
  Since I can't control the file access on the remote server I need a way to do this from my Java program.</p>
  
  <p>Here is the relevant part of the code I wrote:</p>
  
  <pre><code>InputStream contentInputStream = null;
  try {
      contentInputStream = new ByteArrayInputStream(Files.readAllBytes(Paths.get("test1.txt")));
  } catch (IOException e) {
      e.printStackTrace();
  }
  sftpChannel.put(contentInputStream, "abc.txt");
  </code></pre>