Sphere Online Judge, Problem 2: Prime Number Generator
I'll make general statements not addressing (or not limited to) performance.



Your overall design is not very Object-Oriented. Usage of static/global variables is discouraged for a reason, it is very hard to follow the flow the program. Consider rewriting it with proper classes/encapsulation.

You could extract reading into one function, which then returns the list of pairs which another function can work on. Some pseudo code:

main()
    List pairs
    if args provided
        pairs = pairsFromFile(args[0])
    else
        pairs = pairsFromStdIn()

    foreach pair in pairs
        processpair(pair)




Your usage of one-line ifs is partly confusing, consider to expand those to at least two lines for readability:

if(test)
    return true;




You're declaring and actively throwing exceptions which never get caught, that means that your application will exit with a thrown exception, that's a bad thing. Applications should handle exceptions at least in the main loop and exit gracefully.



Consider writing directly to the output-stream instead of buffering everything in a StringBuilder. While it might hurt performance if you're doing a lot of writing, it will remove some of the complexity.



System.out.println("IOException encounterd!");


Exceptions and errors should traditionally go to System.err.



You're using System.exit() at some places, it might be more desirable to let the main loop handle the application flow instead of "exiting" from any or some functions. I also now realize that you're using some sort of template to handle exceptions (write to out, print stacktrace, exit(-1)), please don't just slap an "error-handling-template" on it and be done with it. Handle exceptions in a context-sensitive way.



isPrime() does use a temporary variable isPrime when returning at the appropriate places would be more then enough.

private static boolean isPrime(int num) {
  if (num &lt; 2)
    return false;
  else if (num &gt; 3) {
    // Loop here
        if (num % (6 * factorNdx + 1) == 0 || num % (6 * factorNdx - 1) == 0) {
          return false;
        }
  }

  if (isPrime) primes.add(num);
  return true;
}