title: How to append data in a SFTP server file using Java
body: <p>I have tried all possible ways to append some content to a file present in SFTP file path. I have got success message but not able to find that file updated.
  Not sure what is the reason.</p>
  
  <p>Please find my code below:</p>
  
  <pre><code>jsch = new JSch();
  jsch.addIdentity(privateKey); 
  session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
  session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
  ProxyHTTP  proxy = new ProxyHTTP(PROXY, 8080);
  session.setProxy(proxy);
  
  session.setConfig("StrictHostKeyChecking", "no");
  session.setConfig("cipher.s2c", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
  session.setConfig("cipher.c2s", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
  session.connect();
  
  sftpChannel = (ChannelSftp)session.openChannel("sftp");
  sftpChannel.connect();
  if(sftpChannel.getSession().isConnected())
      logger.debug("Remote session established through the channel");
  
  sftpChannel.cd(remoteDirectory);
  
  List&lt;String&gt; list = new ArrayList&lt;&gt;();
  
  ChannelSftp sftp = (ChannelSftp) sftpChannel;
  Vector&lt;LsEntry&gt; files = sftp.ls(remoteDirectory);
  
  for (LsEntry entry : files)
  {
      if (!entry.getFilename().equals(".") &amp;&amp; !entry.getFilename().equals(".."))
      {
          list.add(entry.getFilename());
      }
  }
  
  System.out.println(list);
  
  InputStream stream = sftp.get(remoteDirectory + remoteFile);
  try {
      BufferedReader br = new BufferedReader(new InputStreamReader(stream));
      String line;
      while ((line = br.readLine()) != null) {
          System.out.println(line);
      }
      // read from br
  } finally {
      stream.close();
  }
  Files.write(Paths.get(remoteFile), "CH|OK|en,ch,de,it|CH ".getBytes(), StandardOpenOption.APPEND);
  System.out.println("added country to remote path");
  sftpChannel.exit();
  session.disconnect();
  </code></pre>