Don't call System.setOut. When you do that, you can no longer print to the console. Instead of System.out.println to write to the file, just... stream.println to write to the file. Then you can use System.out to print to the console.

Answer from rzwitserloot on Stack Overflow
🌐
Codecademy
codecademy.com › docs › java › output › .println()
Java | Output | .println() | Codecademy
November 5, 2022 - The .println() method prints its argument to the console followed by a new line. It is probably the most common method of displaying output to the console. ... Looking for an introduction to the theory behind programming?
Top answer
1 of 2
4

Don't call System.setOut. When you do that, you can no longer print to the console. Instead of System.out.println to write to the file, just... stream.println to write to the file. Then you can use System.out to print to the console.

2 of 2
2

Answer by rzwitserloot is correct. For fun, let's rewrite the code in the Question using the benefits of modern Java. Our rewrite includes the new (preview) class java.io.IO for console access that avoids the error found by rzwitserloot.

java.time

No need to use System.currentTimeMillis() anymore. Supplanted by the java.time classes, specifically Instant.now() which captures the current moment as seen in UTC.

In Java 8, this resolves to milliseconds, while in Java 9 and later this resolves to the finer level microseconds in many implementations.

Instant start = Instant.now() ;

Calculate elapsed time using Duration.

Duration duration = Duration.between ( start , Instant.now() ) ;

java.io.IO

Using preview feature in Java 23 & 24, use new java.io.IO class for easier console use. The IO class has three simple methods: print, println, and readln. See another Question, Newer easier ways to interact with the console in Java 23+?.

Note how we conveniently pass the user-prompt as an argument to IO.readln to do double-duty: Print a prompt on the console, while also taking input from the user.

// Gather inputs.
String fileName = IO.readln ( "Choose a name to your file: " );

String rangeInput = IO.readln ( "Specify our range:" );
int range = Integer.parseInt ( rangeInput );

String arrayInput = IO.readln ( "Specify length of array:" );
int arrayLength = Integer.parseInt ( arrayInput );

// Dump to console
IO.println ( "fileName = " + fileName );
IO.println ( "range = " + range );
IO.println ( "arrayLength = " + arrayLength );

In real work, we would add more code for error-handling. Errors might include the user entering an empty string for file name, or entering characters other than digits for numeric inputs. But we omit that code to keep this demo simple and clear.

When run:

Choose a name to your file: Bogus
Specify our range:42
Specify length of array:7
fileName = Bogus
range = 42
arrayLength = 7

Stream

Write some logic to get your array of distinct values. Using the Stream feature in Java 8+ makes this quite simple.

int[] ints =
        ThreadLocalRandom
                .current ( )
                .ints ( 0 , range )
                .distinct ( )
                .limit ( arrayLength )
                .toArray ( );

ints = [41, 19, 11, 40, 16, 34, 35]

Sequenced collections

Java 21 and later now benefits from sequenced collections, additional interfaces added to the Java Collections Framework. See talk by Stuart Marks. SequencedCollection is a super-interface to List and others.

Convert from our domain data, an array of int primitives, to our data for serialization, a SequencedCollection of String objects.

SequencedCollection < String > lines =
        Arrays
                .stream ( ints )
                .mapToObj ( Integer :: toString )
                .toList ( );

NIO.2

Modern Java offers the NIO.2 features for easier file-handling.

We can easily write a collection of lines of text with a call to Files.write.

Path path = Paths.get ( "/Users/basil_dot_work/" , fileName );
Path resultingPath;
try { resultingPath = Files.write ( path , lines , StandardCharsets.UTF_8 ); }
catch ( IOException e ) { throw new RuntimeException ( e ); }

You asked:

I want to know is it possible to print to the console that the file has been printed when it's done.

In the code above, a Path object is returned by our call to Files.write if we have success. If a file system problem occurs, an exception is thrown. We can print the returned Path object to the console with another IO.println.

Example code

For your copy-paste convenience, here is the entire app.

package work.basil.example.console;

import java.io.IO;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.SequencedCollection;
import java.util.concurrent.ThreadLocalRandom;

public class MakeFile
{
    public static void main ( String[] args )
    {
        // Gather inputs.
        String fileName = IO.readln ( "Choose a name to your file: " );

        String rangeInput = IO.readln ( "Specify our range:" );
        int range = Integer.parseInt ( rangeInput );

        String arrayInput = IO.readln ( "Specify length of array:" );
        int arrayLength = Integer.parseInt ( arrayInput );

        // Start the stopwatch.
        Instant start = Instant.now ( );

        // Domain data.
        int[] ints =
                ThreadLocalRandom
                        .current ( )
                        .ints ( 0 , range )
                        .distinct ( )
                        .limit ( arrayLength )
                        .toArray ( );


        // Serialized data.
        SequencedCollection < String > lines =
                Arrays
                        .stream ( ints )
                        .mapToObj ( Integer :: toString )
                        .toList ( );

        // Write file
        Path path = Paths.get ( "/Users/basil_dot_work/" , fileName );
        Path resultingPath;
        try { resultingPath = Files.write ( path , lines , StandardCharsets.UTF_8 ); }
        catch ( IOException e ) { throw new RuntimeException ( e ); }

        // Dump to console
        IO.println ( "fileName = " + fileName );
        IO.println ( "range = " + range );
        IO.println ( "arrayLength = " + arrayLength );
        IO.println ( "ints = " + Arrays.toString ( ints ) );
        IO.println ( "lines.toString(): " + lines );
        IO.println ( "path: " + path );
        IO.println ( "resultingPath = " + resultingPath );
        IO.println ( "Elapsed: " + Duration.between ( start , Instant.now ( ) ) );

    }
}

When run on Java 23.0.1 from IntelliJ IDEA 2024.3 Beta (Ultimate Edition) on a MacBook Pro with M1 Pro Apple Silicon on macOS Sonoma 14.7.1.

Choose a name to your file: Bogus
Specify our range:42
Specify length of array:7
fileName = Bogus
range = 42
arrayLength = 7
ints = [40, 37, 8, 4, 18, 41, 24]
lines.toString(): [40, 37, 8, 4, 18, 41, 24]
path: /Users/basil_dot_work/Bogus
resultingPath = /Users/basil_dot_work/Bogus
Elapsed: PT0.002611S
🌐
W3Schools
w3schools.com › java › ref_output_println.asp
Java Output println() Method
Java Examples Java Videos Java ... Java Syllabus Java Study Plan Java Interview Q&A ... The println() method prints text or values to the console, followed by a new line....
🌐
GeeksforGeeks
geeksforgeeks.org › java › system-out-println-in-java
System.out.println in Java - GeeksforGeeks
Used to display text, variables, and expressions on the console. Automatically adds a newline after printing the output. Commonly used for debugging, testing, and displaying program results.
Published   June 2, 2026
🌐
CodeGym
codegym.cc › java blog › random › how to print output to console in java
How to Print output to Console in Java
April 3, 2025 - Quick tests: Want to check if a method works? Just print the result to the console. ... Run this, and you'll see "Hello, Java world!" in your console, followed by a new line. That "ln" in println stands for "line," and it's what gives you that extra space.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 411269 › java-console-output
Java Console Output | DaniWeb
February 10, 2012 - I thought you meant you wanted to read data in from the console as a user entered it. ... For debugging: Print the String: blink to see what it contains.
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-print-output-to-console-in-java-417652
How to print output to console in Java | LabEx
The console, also known as the ... Java, the primary method for printing output to the console is through the System.out.println() method....
🌐
Reddit
reddit.com › r/vscode › java console output
r/vscode on Reddit: Java console output
April 21, 2022 -

Hello everyone!

I am new to using VS Code and there are some things that I am getting used to. I used to code in Replit and CodeHS before and it is my first time using this platform. However, there are some things that I am not used to - like for example when I code System.out.println("Hello World!") VS Code prints out the entire file directory and everything. Is there a way that I can change this so that I only get a simple "Hello World!"? It is mostly more of a visual aesthetic for me than anything else. Thank you all in advance.

🌐
CodeHS
codehs.com › tutorial › 13575
Tutorial: Printing in Java | CodeHS
Learn how to print values to the console using Java!
🌐
iO Flood
ioflood.com › blog › system-out-println
Java's System.out.println Explained: How does it Work?
February 20, 2024 - Its main function is to print text to the console, which can be incredibly useful for debugging, displaying program outputs, or just simple greetings. Here’s a simple step-by-step guide on how to use the system.out.println command: Open your ...
🌐
JDoodle
jdoodle.com › online-java-compiler
JDoodle - Online Compiler & IDE for 110+ Languages | Free
Write and execute Java code online using JDoodle's Free Java online compiler.
🌐
Visual Studio Code
code.visualstudio.com › docs › java › java-tutorial
Getting Started with Java in VS Code
November 3, 2021 - This tutorial shows you how to write and run Hello World program in Java with Visual Studio Code.
🌐
Coderanch
coderanch.com › t › 389825 › java › Printing-line-console
Printing on the same line in the console (Beginning Java forum at Coderanch)
September 25, 2001 - Let me give you an example of what I am looking for : if you have ever formatted a disk from the dos console you would remember that the percent completed would refresh on the same line. I have been looking for code which does the same i.e. prints on the same line or rather updates that very line instead of printing on a new lineon every print call. ... System.out.println() includes a newline action. System.out.print() stays on the same line. "JavaRanch, where the deer and the Certified play" - David O'Meara
🌐
Dev.java
dev.java › learn › getting-started
Getting Started with Java - Dev.java
One you have opened the MyFirstJavaApp.java file, you can just copy and paste the following code in it. As you may know, there is a long-standing tradition in computer science, which is to write a program that prints "Hello, World!" on the console of your application.
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-print-and-println-in-java
print vs println in Java - GeeksforGeeks
1 month ago - In Java, print() and println() are methods of the PrintStream class used to display output on the console. Both methods print data such as strings, numbers, characters, and objects, but they differ in how they handle the cursor position after ...
🌐
W3Schools
w3schools.com › java › java_output.asp
Java Output Values / Print Text
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... You learned from the previous chapter that you can use the println() method ...
🌐
Spiceworks
community.spiceworks.com › programming & development
How to Send Console Output to a HTML Page - Programming & Development - Spiceworks Community
March 24, 2009 - I wrote a Java program. In that, I have some statements which are printing the output to console using “System.out.println().” Now, I want to send these output statements to a HTML page. Please help me regarding this iss…