String outputFile = "primes.dat";
if (args.length > 0) outputFile = args[0];
int numPrimes;
try {
numPrimes = Integer.parseInt(args[1]);
}
catch (Exception e) {
numPrimes = 100;
}
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(numPrimes*4);
DataOutputStream dout = new DataOutputStream(bout);
int lastPrime = 1;
for (int i = 0; i < numPrimes; i++) {
lastPrime = nextPrime(lastPrime);
dout.writeInt(lastPrime);
}
FileOutputStream fout = new FileOutputStream(outputFile);
fout.write(bout.toByteArray());
fout.flush();
fout.close();
}
catch (IOException e) {
System.err.println(e);
}