title: Downloading a file from a remote server via SFTP using OutputStream (android)
body: <p>I'm trying to download a file from a remote server using SFTP as directed in a tutorial <a href="http://kodehelp.com/java-program-for-downloading-file-from-sftp-server/" rel="nofollow noreferrer">here</a></p>
  <p>Everything seems to be fine during the InputStream, but when it gets to the OutputStream, it crashes and gives me a trace error of:</p>
  <blockquote>
  <p>java.io.FileNotFoundException: /Internal Storage/Documents/sample.txt:
  open failed: ENOENT (No such file or directory)</p>
  <p>Caused by:
  libcore.io.ErrnoException: open failed: ENOENT (No such file or
  directory)</p>
  </blockquote>
  <p>Any help?</p>
  <p>here is my code:</p>
  <pre><code>public class MainActivity extends Activity {
  
      private static final String SFTPWORKINGDIR = &quot;/path/to/file&quot;;
      private String user = &quot;username&quot;;
      private String pass = &quot;password&quot;;
      private String host = &quot;hostname&quot;;
      private int portNum = 22;
  
      private String fileName = &quot;sample.txt&quot;;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          new AsyncTask&lt;Void, Void, List&lt;String&gt;&gt;() {
              @Override
              protected List&lt;String&gt; doInBackground(Void... params) {
                  try {
                      Downloader(fileName);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  return null;
              }
  
          }.execute();
      }
  
      public void Downloader(String fileName) {
  
          JSch jsch = new JSch();
          Session session = null;
          Channel channel = null;
          ChannelSftp sftpChannel = null;
  
          try {
  
              session = jsch.getSession(user, host, portNum);
              session.setConfig(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
              session.setPassword(pass);
              session.connect();
  
              channel = session.openChannel(&quot;sftp&quot;);
              channel.connect();
              sftpChannel = (ChannelSftp) channel;
              sftpChannel.cd(SFTPWORKINGDIR); //cd to dir that contains file
  
              try {
                  byte[] buffer = new byte[1024];
                  BufferedInputStream bis = new BufferedInputStream(sftpChannel.get(fileName));
                  File newFile = new File(&quot;some/file/&quot;);
                  OutputStream os = new FileOutputStream(newFile); //CRASHES HERE
                  BufferedOutputStream bos = new BufferedOutputStream(os);
                  int readCount;
                  while( (readCount = bis.read(buffer)) &gt; 0) {
                      Log.d(&quot;Downloading&quot;, &quot; &quot; + fileName );
                      bos.write(buffer, 0, readCount);
                  }
                  bis.close();
                  bos.close();
  
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              Log.d( &quot; &quot;, fileName + &quot; has been downloaded. MAYBE&quot;);
  
              sftpChannel.exit();
              session.disconnect();
  
          } catch (JSchException e) {
              e.printStackTrace();
          } catch (SftpException e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>