I wrote this program and while it does everything it should, but I am not sure if it is correct. I need the program to take all the even numbers and write to a .dat file. Then I need to close the file, reopen it, and display the results. After that I need to get the odd numbers and add them to the end of the file. Then reopen the file and display the results. Is this correct?
import java.io.*;
import java.util.*;
public class Numbers {
// The main function
private static FileOutputStream output;
private static FileInputStream input;
private static ArrayList<Integer> inputList;
static int sumEven = 0;
static int sumOdd = 0;
public static void main(String[] args) throws IOException
{
createOutputFileEven();
createInputFile();
printInputList();
createOutputFileOdd();
createInputFile();
printInputList();
}
private static void createOutputFileEven() throws IOException
{
//create the output file
output = new FileOutputStream("numbers.dat");
//output test values to the file
for(int i = 2; i <= 100; i +=2)
{
sumEven = sumEven + i;
output.write(i);
}
System.out.print("The sum of the Even numbers is " + sumEven + ".\n");
output.close();
}//end private static void creatOutputFile
private static void createOutputFileOdd() throws IOException
{
//create the output file
output = new FileOutputStream("numbers.dat");
//output test values to the file
for(int i = 1; i < 100; i += 2)
{
sumOdd =sumOdd + i;
output.write(i);
}
//close the output stream
System.out.print("The sum of the Even numbers is " + sumOdd + ".\n");
output.close();
}//end private static void creatOutputFile
private static void createInputFile() throws IOException
{
inputList = new ArrayList<Integer>(10);
int value;
input = new FileInputStream("numbers.dat");
while ((value = input.read()) != -1)
inputList.add(value);
}
private static void printInputList()
{
String output;
if (inputList.size() > 0)
{
output = "The values are:\n";
for(int i = 0; i < inputList.size(); i++)
if (i == 0)
output += inputList.get(i);
else
output += ", " + inputList.get(i);
}
else
{
output = "No values in the input list";
}
System.out.println(output);
}
}