title: Writing output to text file in a class in java?
body: <p>I'm having an issue of writing output to a text file. I'm not too sure on how to go about it. I've tried to write output by writing something along the lines of- Writer is an object of the printWriter class:</p>
  
  <p>This is trimmed down substantially but it boils down to.</p>
  
  <pre><code>Writer.println(sortRI());
  </code></pre>
  
  <p>I can't write that statement as the method itself- and all methods in this class I'm trying to write to an output file for are void. </p>
  
  <p>So my question is how do I write output to a text file from my methods which are void- they print out their results via a few print statements. Is there a way I can instantiate a writer and use the writer to print the methods. An example of one of the methods is shown below:</p>
  
  <p>ZipCodeLocation[] data2 = new ZipCodeLocation[];</p>
  
  <pre><code>public class Processor
  {
    ...
  
    public void readData(){
  
    ...
  
    }
  </code></pre>
  
  <hr>
  
  <pre><code>public void findSpringfield()
  {
      System.out.println("------------------------------------------------------------------------");
      System.out.println("SPRINGFIELD ZIP CODES");
  
      for(int i=0; i &lt; data2.length ; i++)
      {
          if(data2[i].getpostalCity().replaceAll("\"","").contains("SPRINGFIELD"))
          {
            System.out.println(data2[i].getzipCode().replaceAll("\"",""));  //Prints out zipCodes that have Springfield in them
          }
      }
  }
  </code></pre>
  
  <p>Instead of printing the message to System.out in the console, how could I have it write the output of the method to a text file? </p>