title: File write/read function like in PHP
body: <p>So I'm learning new things day by day in Java, and I hope one day I should have same knowledge in Java as in PHP. </p>
  
  <p>I'm trying to make a class that is similar to <code>fopen</code>, <code>fwrite</code>, <code>fclose</code> in PHP like:</p>
  
  <pre><code>&lt;?php
  $fp = fopen('data.txt', 'w');
  fwrite($fp, '1');
  fwrite($fp, '23');
  fclose($fp);
  
  // the content of 'data.txt' is now 123 and not 23!
  ?&gt;
  </code></pre>
  
  <p>I also need the method of writing</p>
  
  <p><strong>o</strong> - for delete and write/overwrite</p>
  
  <p><strong>a</strong> - for append at end</p>
  
  <p>and a read function that returns the the content line by line, so I can put it into an array , like <strong>file_get_contents(file);</strong></p>
  
  <p>This is what I have so far ...</p>
  
  <pre><code>import java.io.*;
  import java.util.Scanner;
  
  /** 
  Read and write a file using an explicit encoding.
  Removing the encoding from this code will simply cause the 
  system's default encoding to be used instead.  
  **/
  public final class readwrite_txt 
  {
  
      /** Requires two arguments - the file name, and the encoding to use.  **/
      public static void main(String[] args) throws IOException 
      {
          String fileName = "text.txt";
          String encoding = "UTF-8";
  
          readwrite_txt test = new readwrite_txt(fileName,encoding);
          test.write("argument.txt","some text","UTF-8","o");
      }
  
      /** Constructor. **/
      readwrite_txt(String fileName, String encoding)
      {
          String fEncoding = "text.txt";
          String fFileName = "UTF-8";
      }
  
      /** Write fixed content to the given file. **/
      public void write(String fileName,String input,String encoding,String writeMethod) throws IOException  
      {
          // Method overwrite
          if(writeMethod == "o")
          {
              log("Writing to file named " + fileName + ". Encoding: " + encoding);
              Writer out = new OutputStreamWriter(new FileOutputStream(fileName), encoding);
              try 
              {
                  out.write(input);
              }
              finally 
              {
                  out.close();
              }
          }
      }
  
      /** Read the contents of the given file. **/
      public void read(String fileName,String output,String encoding,String outputMethod) throws IOException 
      {
          log("Reading from file.");
          StringBuilder text = new StringBuilder();
          String NL = System.getProperty("line.separator");
          Scanner scanner = new Scanner(new FileInputStream(fileName), encoding);
          try 
          {
              while (scanner.hasNextLine())
              {
              text.append(scanner.nextLine() + NL);
              }
          }
          finally
          {
              scanner.close();
          }
          log("Text read in: " + text);
      }
  
      // Why write System.out... when you can make a function like log("message"); simple!
      private void log(String aMessage)
      {
      System.out.println(aMessage);
      }
  }
  </code></pre>
  
  <p>also, I don't understand why I must have</p>
  
  <pre><code>readwrite_txt test = new readwrite_txt(fileName,encoding);
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>readwrite_txt test = new readwrite_txt();
  </code></pre>
  
  <p>I just want to have an simple function similar to that in PHP.</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>So my function must be</p>
  
  <pre><code>$fp = fopen('data.txt', 'w'); ==&gt; readwrite_txt test = new readwrite_txt(filename,encoding,writeMethod);
  
  fwrite($fp, '23');            ==&gt; test.write("the text");
  
  fclose($fp);                  ==&gt; ???
  </code></pre>