You're using a DataOutputSteam
which is generally used to write out binary data. If you want the data written into a file in plan text form, then instantiate your output like so:
PrintWriter out = new PrintWriter(new FileWriter("PrimeNumbers.txt"));
Then, when it comes time to output, do this:
out.println(n);
out.flush();
And then close it when done like so:
out.close();
This will print one per line; not sure if this is exactly what you want.
Also, I noticed in your code that your writing to DataOS outside of the if
statement that checks if a number is prime, meaning that you will be writing all numbers to the file and not just prime numbers.
If you need to follow the same convention in the file as you do for System.out
, then change the code to look like this:
if (count % NUMBER_PER_LINE == 0)
{
System.out.println(n);
out.println(n);
}
else{
System.out.print(n + " ");
out.print(n + " ");
}
out.flush();