java – Read and sort 100 numbers from a text file

File in = new File("input.txt");

Do not shorten variable names, it makes the code harder to understand and maintain.


        for (int i = 0; i < numbers.length; i++) {
            numbers(i) = input.nextInt();
        }

I’m a very persistent advocate that you’re only allowed to use single-letter variable names if you’re dealing with dimensions. Even for simple for loops one should use index or counter or something similar descriptive.


    try {
        Scanner input = new Scanner(in);

Could have used a try-with-resources instead.


System.exit(1);

Just as a note, that is not “let’s exit the application” kind of call, it’s “let’s end the JVM abruptly” kind of call. When calling System.exit, not even finally blocks will be executed.


for (int i = 0; i < numbers.length; i++) {

You could use a for-each instead.


            System.err.printf("File name does not denote an existing, writable regular file %n"
                            + "and a new regular file of that name cannot be created%n"
                            + "Or another error occured creating/writing to the specified file.%n"
                            + "Exiting with code: 2");

That’s completely unhelpful error handling and is on the same level as Microsoft Windows’ infuriating “Your PC had a problem :)” message. Never throw away error information that you have, it will make debugging a lot harder. Printing the exception message would be a minimum that you should do because from that error message, even though you did put a lot of thought and work into it, it’s completely impossible to deduce the actual error and what to do to remedy it.

Short story time: I’ve been working with a third-party service lately and that service tries to contact yet another service. Whenever there is a problem with the communication with the other service, the service will spew out the log message “Failed to communicate with that other service, is the other service really already started?” followed by the exception message that clearly shows that an HTTP response was received. Not helpful, because you know that somebody will get hung up at some point on that message even though the question is bogus.


Coming around to your actual question, if you put your logic into a method of its own, your question simply goes away.

public static final void sortContent(File inputFile, File outputFile) throws IOException {
    // TODO Add verification that inputFile is not null.
    // TODO Add verification that outputFile is not null.

    int() numbers = new int(100);
    
    try (Scanner scanner = new Scanner(inputFile) {
        // Logic goes here.
    }

    // Rest of logic goes here.
}

Now all you need to worry about is closing the resources correctly, which the try-with-resources will do for you. The IOException will simply being thrown and bubble up until it is being handled. For example in your main method:

public static final void main(String() args) {
    try {
        sortContent(new File("input.txt"), new File("output.txt"));
    } catch (IOException e) {
        ex.printStackTrace();
    }
}

If you feel especially inclined to provide more information, you can always wrap the thrown exception.

public static final void sortContent(File inputFile, File outputFile) throws IOException {
    // ...
    
    try (Scanner scanner = new Scanner(inputFile) {
        // ...
    } catch (IOException e) {
        throw new IOException("Failed to read from input file <" + inputFile.toString() + ">.", e);
    }

    // ....
}

Notice that we do not throw away the original exception, but instead use it as cause for the one we are throwing.