title: Write String to a text file
body: <p>I have this code that gets GPS data from input stream make the test file but there is nothing inside. Can you please tell me what is the problem in here? </p>
  
  <pre><code>public void run ()
          {
              byte[] buffer = new byte[1024];
              int len = -1;
              try
              {
                  while ( ( len = this.in.read(buffer)) &gt; -1 )
                  {
                      String gpsInfo = new String(buffer,0,len);
                      PrintWriter data = new PrintWriter("test.txt");
                      data.println(gpsInfo);
                      System.out.print(gpsInfo);
                  }
              }
              catch ( IOException e )
              {
                  e.printStackTrace();
              }            
          }
  </code></pre>
  
  <p>I am getting the right console output.</p>
  
  <p><a href="https://i.stack.imgur.com/OEeoq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OEeoq.png" alt="enter image description here"></a></p>