Java Reference
In-Depth Information
I'm just showing the new code that replaces the code in the previous example here. This is the code that
allocates the buffers and writes the file:
// import statements as before...
public class PrimesToFile3 {
public static void main(String[] args) {
int primesRequired = 100; // Default count
if (args.length > 0) {
try {
primesRequired = Integer.valueOf(args[0]).intValue();
} catch (NumberFormatException e) {
System.out.println("Prime count value invalid. Using default
of "
+ primesRequired);
}
}
long[] primes = new long[primesRequired]; // Array to store
primes
getPrimes(primes); // Calculate the
primes
Path file = createFilePath("Beginning Java Stuff",
"primesAgain.txt");
writePrimesFile(primes, file); // Write the file
}
// getPrimes() method as in the previous example...
// createFilePath() method as in the previous example...
// Write the array contents to file
private static void writePrimesFile(long[] primes, Path file) {
final int BUFFERSIZE = 1024;
// Buffer size in bytes
- bigger!
try (WritableByteChannel channel = Files.newByteChannel(
file, EnumSet.of(WRITE,
Search WWH ::




Custom Search