title: Writing even and odd numbers to a dat file
body: <p>I have started learning Java and am having problem with a simple program that should write some content to <code>numbers.dat</code> file.</p>
  <p>I am facing two problems:</p>
  <ol>
  <li><p>The output that I am getting is as follows:</p>
  <blockquote>
  <p>2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056, 1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070, 2162, 2256, 2352, 2450, 2550...</p>
  </blockquote>
  <p>When it should be:</p>
  <p>For even numbers:</p>
  <blockquote>
  <p>2,4,6,8,10,12,14,……,98,100</p>
  </blockquote>
  <p>For odd numbers:</p>
  <blockquote>
  <p>2,4,6,8,10,12,14,……,98,100,1,3,5,7,9,……..97,99</p>
  </blockquote>
  </li>
  <li><p>My second problem is that the output is not getting written to <code>numbers.dat</code> file! It's all blank.</p>
  </li>
  </ol>
  <p>The question from the book that I am trying to solve is:</p>
  <blockquote>
  <p>Write a program called TextFileIO.java to create a file named numbers.dat. Then create an algorithm that writes all even numbered integers from 1 to 100, separated by a comma.</p>
  <p>After the file has been created, close and reopen the file and display the results to the screen. After the results have been displayed append the odd number integers from 1 to 100, separated by a comma to the end of the file. Reopen the file and display the results. The contents of the file should be the even numbers from 1 to 100 separated by a comma followed by the odd number from 1 to 100 separated by a comma. The output of this program would be something like the following:</p>
  <p>2,4,6,8,10,12,14,……,98,100</p>
  <p>2,4,6,8,10,12,14,……,98,100,1,3,5,7,9,……..97,99</p>
  </blockquote>
  <p>Below is the code that I have tried so far:</p>
  <p><strong>textFileIO.java</strong></p>
  <pre><code>package package1;
  
  import java.io.*;
  import java.util.*;
  
  //class definition
  public class textFileIO{
  
      //The main function
      public static void main(String args[]) {
  
          //This declares a file to open.
          File outFile = new File(&quot;numbers.dat&quot;);
  
          int sumEven = 0;
          int sumOdd = 0;
  
          //File IO requires a try/catch block to prevent the program from crashing
          try {
  
              //a buffered writer is used to allow us to write to the file.
              BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));
  
              //This for loop handles adding all of the numbers together
              for(int i = 1; i &lt; 100; i += 2) {
                  sumEven += i + 1;
                  writer.write(&quot;&quot; + sumEven + &quot;, &quot;);
              }
  
              //adding a new line and closing the file
              writer.newLine();
              writer.close();
  
              //A buffered Reader is used for reading a new file.
              BufferedReader reader = new BufferedReader(new FileReader(outFile));
  
              //Because I only add a new line at the end, I only need to read the first line.
              System.out.println(reader.readLine());
  
              //I need to write again, so I close the reader and open the writer.
              reader.close();
  
              //you need to create another object to append to the file
              //writer.open();
              BufferedWriter writer2 = new BufferedWriter(new FileWriter(outFile));
  
              //This for loop is identical to the previous one, except for odd numbers
              for(int i = 1; i &lt; 100; i += 2) {
                  sumOdd += i;
                  writer2.write(&quot;&quot; + sumOdd + &quot;, &quot;);
              }
  
              writer.newLine();
              writer.close();
  
              //The same here. You need to create another reader
              //reader.open();
              BufferedReader reader2 = new BufferedReader(new FileReader(outFile));
              System.out.println(reader2.readLine());
  
          }
          catch (Exception e) {
  
          }
      }
  }
  </code></pre>
  <p><strong>Edit 2</strong></p>
  <p>I changed my code and this is my latest code. I am still having problems.
  The console output screen is showing 49 and 50 as output and my numbers.dat file is now showing only comma separated list of odd numbers. What am I doing wrong now?</p>
  <pre><code>import java.io.*;
      import java.util.*;
      public class TextFileIO {
          public static void main(String[] args) throws Exception {
              //Clear the contents of numbers.dat if already exists and populated
              File fileOne = new File(&quot;numbers.dat&quot;);
              fileOne.delete();
              File newFile = new File(&quot;numbers.dat&quot;);
              newFile.createNewFile();
              //Create writer object
              PrintWriter writer = new PrintWriter(new FileWriter(&quot;numbers.dat&quot;));
              //Loop from 1 to 100
              for (int i = 1; i &lt;= 100; i++) {
                  //If number is even, write to file  
                  if (i % 2 == 0) {
                      writer.print(i + &quot;,&quot;);
                  }
              }
              //Close writer object.
              writer.close();
              //Read from file
              FileInputStream fileStream = new FileInputStream(&quot;numbers.dat&quot;);
              DataInputStream in = new DataInputStream(fileStream);
              BufferedReader reader = new BufferedReader(new InputStreamReader(in));
              //Print line
              System.out.println(reader.read());
              reader.close();
              //Write all odd numbers to file
              PrintWriter writer2 = new PrintWriter(new FileWriter(&quot;numbers.dat&quot;));
              //Loop from 1 to 100
              for (int i = 1; i &lt;= 100; i++) {
                  //If number is odd, write to file  
                  if (i % 2 == 1) {
                      writer2.print(i + &quot;,&quot;);
                  }
              }
              writer2.close();
              //Read from file
              FileInputStream fileStream2 = new FileInputStream(&quot;numbers.dat&quot;);
              DataInputStream in2 = new DataInputStream(fileStream2);
              BufferedReader reader2 = new BufferedReader(new InputStreamReader(in2));
              //Print line
              System.out.println(reader2.read());
              reader.close();
          }
       
       
      }
  </code></pre>
  <p><strong>Edit 3:</strong> I am still unable to get the desired output. I am still getting the output in console as 49 and 50, and in numbers.dat file, the output is still only odd numbers separated by commas. I am unable to understand what exactly to change in my code.</p>