title: Creating, writing to and reading files
body: <p>I know there are many questions related to this, but I still do not follow. I have copied the below code from a tutorial on how to create, write to and read from a file. There is a <code>CreateFile</code> class, a <code>ReadFile</code> class and a <code>Demo</code> class:</p>
  
  <p>CreateFile.java</p>
  
  <pre><code>import java.io.*;
  import java.lang.*;
  import java.util.*;
  
  public class CreateFile {
      private Formatter x;
  
      public void openFile(){
          try{
              x = new Formatter("chinese.txt");
          }
          catch(Exception e)
          {
              System.out.println("You have an error");
          }
      }
  
      public void addRecords(){
          x .format("%s%s%s", "20 ", "bucky ", "robers");
      }
  
      public void closeFile(){
          x.close();
      }
  }
  </code></pre>
  
  <p>ReadFile.java</p>
  
  <pre><code>public class ReadFile {
      private Scanner x;
  
      public void openFile()
      {
          try{
              x = new Scanner(new File("words.txt"));
          }
          catch(Exception e){
              System.out.println("could not find file");
          }
      }
  
      public void readFile()
      {
          while(x.hasNext()) 
          {
              String a = x.next();
              String b = x.next();
              String c = x.next();
  
              System.out.printf("%s %s %s\n", a,b,c);
          }
      }
      public void closeFile()
      {
          x.close();
      }
  
  }
  
  
  public class Demo {
  
      public static void main(String[] args) {
          CreateFile g = new CreateFile();
          g.openFile();
          g.addRecords();
          g.closeFile();
  
          WordCounter r = new WordCounter();
          r.openFile();
          r.readFile();
          r.closeFile();
      }
  </code></pre>
  
  <p>In <code>Demo.java</code> if I remove the last four statements related to reading the file, the first four statements related to opening and writing to a file run without error. However, once I add          </p>
  
  <pre><code>WordCounter r = new WordCounter();
  r.openFile();
  r.readFile();
  r.closeFile();
  </code></pre>
  
  <p>and run the program, it outputs: <code>Exception in thread "main" could not find file.</code> I am not sure what is going on, is the file <code>chinese.txt</code> never being created? </p>