Tuesday, April 30, 2024
HomeJava10 Examples Of Printstream print(), println() and prinf() In Java

10 Examples Of Printstream print(), println() and prinf() In Java


Howdy guys, if you’re working in Java or simply began with Java then you’ve got should come throughout statements like System.out.println(“Howdy world”) to print one thing on console or command immediate. That is really the primary assertion I wrote in Java once I began programming and at the moment I did not understand the way it work and what’s System and PrintStream class and what’s out right here however now I do know and I’m going to elucidate all this to you on this article, together with PrintStream class and its varied print strategies like print(), println() and significantly printf() to print varied objects in Java. However,   Earlier than we get to the ten examples of Printstream printf() in Java,
let me let you know a bit bit extra about what precisely Printstream is. 

Printstream
is mainly an output stream in Java that provides you varied strategies so
that you may print representations of varied information factors
concurrently. Essentially the most generally used output stream in Java,
System.out, is a Printstream. 

You may consider this as an
occasion of the Printstream class. It may possibly additionally characterize a typical
output stream that can be utilized to print textual content output onto the console.
One other occasion of the Printstream class is System.err. Printstream is
additionally a byte outstream. It may possibly convert all of the output into bytes with the
assist of the desired encoding. This may be finished utilizing charset within the
PrintStream()
constructor invocation. 

Printstream in Java can also be
not restricted to a console. It may possibly act as a filter output stream that may
be linked to different output streams. It supplies strategies to print any
information values or objects in an acceptable format. These strategies will
by no means throw an IOException when writing information to an underlying output. 

10 Examples Of Printstream print(), println() and prinf() In Java

1. PrintStream Class Declaration

Since
PrintStream is a concrete subclass of
FilterOutputStream, it may well prolong
the OutputStream superclass. It may possibly additionally implement Closeable,
Flushable, Appendable, in addition to AutoCloseable interfaces. You should utilize
the next syntax to declare the PrintStream class in Java:

public class PrintStream

  extends FilterOutputStream

     implements Appendable, Closeable

The
PrintStream class was first launched within the Java 1.0 model. You may
discover it within the java.io.PrintStream bundle. The PrintStream class can
implement an Appendable interface in Java 5.0, permitting you to make use of it
with a java.util.Formatter. 

2. PrintStream Class Constructor

  • The PrintStream class can outline all kinds of constructors in Java. 
  • The
    PrintStream(File file) constructor can create a brand new print-stream alongside
    with a specified file object and charset with none automated line
    flushing. 
  • The PrintStream(File file, Charset charset)
    constructor creates a brand new print-stream with a specified file object and a
    charset object with none automated line flushing. 
  • The PrintStream(OutPutStream os) constructor can even create a brand new print stream with none automated line flushing. 

3. PrintStream Strategies In Java

The PrintStream can even outline some helpful strategies apart from these derived from the FilterOutPutStream class. 

The PrintStream append​(char c) appends the desired character to this output stream and returns the stream.

The
PrintStream append​(CharSequence cseq, int start, int finish) appends a
subsequence of the desired character sequence to this output stream
and returns the stream. The subsequence begins from the index start and
extends to the character at index end-1.

The PrintStream
printf​(String format, Object… args) 
writes a formatted string to the
underlying output stream utilizing the desired format string and
arguments. It returns the output stream.

4. Create A PrintStream Utilizing Output Streams

You
can create a print stream that may simply write formatted information right into a
file. Earlier than making a PrintStream, you must import the
java.io.PrintStream bundle. 

// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream(String file);

// Creates a PrintStream
PrintStream output = new PrintStream(file, autoFlush);

5. Create A PrintStream utilizing Filename

You
can create a PrintStream with the assistance of a file identify that may write
information into that specified file. You may as well use an non-obligatory boolean
parameter that may specify whether or not to carry out autoflush or not. 

 // Creates a PrintStream
PrintStream output = new PrintStream(String file, boolean autoFlush);

6. The print() methodology

The
print() methodology can be utilized to print the desired information to the output
stream. You may as well use the println() methodology to print the info to the
output stream with a brand new line character on the finish. 

class Major {
    public static void essential(String[] args) {

        String information = "Howdy World.";
        System.out.print(information);
    }
}

On this case, you do not want to create a PrintStream as you should utilize the print() methodology from the PrintStream class. 

7. The printf() Methodology

You
can use the printf() methodology to print a formatted string that can
embody a formatted string and arguments as two parameters. 

printf("I'm %d years previous", 25);

You may see the next instance to get an concept of how you can use printf() in a program. 

import java.io.PrintStream;

class Major {
    public static void essential(String[] args) {

        strive {
            PrintStream output = new PrintStream("output.txt");

            int age = 25;

            output.printf("I'm %d years previous.", age);
            output.shut();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

8. print() Methodology With The PrintStream Class

import java.io.PrintStream;

class Major {
    public static void essential(String[] args) {

        String information = "This can be a textual content contained in the file.";

        strive {
            PrintStream output = new PrintStream("output.txt");

            output.print(information);
            output.shut();
        }
        catch(Exception e) {
            e.getStackTrace();
        }
    }
}

On this instance, a print stream named output is created. This print stream is then linked with an output.txt file. 

9. Print Knowledge With PrintStream

If you wish to print information to a file, you should utilize the straightforward print() methodology. For instance, 

PrintStream output = new PrintStream("output.txt");

Whenever you run this code, the output.txt file that you’ve created might be full of information. 

10. Print Knowledge Utilizing The printf() Methodology

As well as, you may also use the printf() methodology to print formatted textual content right into a file. 

PrintStream output = new PrintStream("output.txt");

Whenever you run this piece of code, the output.txt file might be full of the required content material. 

Ceaselessly Requested Questions

1. What’s PrintStream in Java?

Printstream
is mainly an output stream in Java that provides you varied strategies so
that you may print representations of varied information factors
concurrently. Essentially the most generally used output stream in Java,
System.out, is a Printstream. You may consider this for example of
the Printstream class. It may possibly additionally characterize a typical output stream
that can be utilized to print textual content output onto the console. 

One other occasion
of the Printstream class is System.err. Printstream can also be a byte
outstream. It may possibly convert all of the output into bytes with the assistance of the
specified encoding. This may be finished utilizing charset within the PrintStream()
constructor invocation. 

2. What’s using PrintStream?

Printstream
in Java can also be not restricted to a console. It may possibly act as a filter output
stream that may be linked to different output streams. It supplies
strategies to print any information values or objects in an acceptable format.
These strategies won’t ever throw an IOException when writing information to an
underlying output. 

That is all about PrintStream class and its varied print(), println(), and printf() strategies. They’re very helpful and possibly the primary few class and methodology you’ll use while you begin with Java Programming. If you happen to preferred this checklist of 10 examples of PrintStream in Java, be happy to share it along with your family and friends.  You probably have any doubts, then be happy to ask in feedback. 

Different Java IO tutorials you might like:

  • 2 methods to learn a textual content file in Java? (resolution)
  • What’s distinction between BufferedReader and Scanner in Java (instance)
  • learn an Excel file in Java? (resolution)
  • learn a CSV file in Java? (instance)
  • create a file and listing in Java? (reply)
  • learn an XML file in Java? (reply)
  • append textual content to an current file in Java? (instance)
  • 5 Free Java 8 and Java 9 Programs for Programmers (programs)
  • 5 Free Knowledge Construction and Algorithm Programs (programs)
  • 5 Free programs to be taught Spring Core and Spring Boot for Java builders (programs)
  • 10 tricks to grow to be a greater Java developer (ideas)

If you happen to like this tutorial, do not forget to share and present your love, it inspire me to maintain working exhausting. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments