Hello guys, if you are working in Java or just started with Java then you have must come across statements like System.out.println("Hello world") to print something on console or command prompt. This is actually the first statement I wrote in Java when I started programming and at that time I didn't realize how it work and what is System and PrintStream class and what is out here but now I know and I am going to explain all this to you in this article, along with PrintStream class and its various print methods like print(), println() and particularly printf() to print various objects in Java. But, Before we get to the 10 examples of Printstream printf() in Java, let me tell you a little bit more about what exactly Printstream is.
Printstream is basically an output stream in Java that gives you various methods so that you can print representations of various data points simultaneously. The most commonly used output stream in Java, System.out, is a Printstream.
You can think of this as an instance of the Printstream class. It can also represent a standard output stream that can be used to print text output onto the console. Another instance of the Printstream class is System.err. Printstream is also a byte outstream. It can convert all the output into bytes with the help of the specified encoding. This can be done using charset in the PrintStream() constructor invocation.
Printstream in Java is also not limited to a console. It can act as a filter output stream that can be connected to other output streams. It provides methods to print any data values or objects in an appropriate format. These methods will never throw an IOException when writing data to an underlying output.
1. PrintStream Class Declaration
public class PrintStream
extends FilterOutputStream
implements Appendable, Closeable
2. PrintStream Class Constructor
- The PrintStream class can define a wide variety of constructors in Java.
- The PrintStream(File file) constructor can create a new print-stream along with a specified file object and charset without any automatic line flushing.
- The PrintStream(File file, Charset charset) constructor creates a new print-stream with a specified file object and a charset object without any automatic line flushing.
- The PrintStream(OutPutStream os) constructor can also create a new print stream without any automatic line flushing.
3. PrintStream Methods In Java
4. Create A PrintStream Using Output Streams
// Creates a FileOutputStream FileOutputStream file = new FileOutputStream(String file); // Creates a PrintStream PrintStream output = new PrintStream(file, autoFlush);
5. Create A PrintStream using Filename
// Creates a PrintStream PrintStream output = new PrintStream(String file, boolean autoFlush);
6. The print() method
class Main { public static void main(String[] args) { String data = "Hello World."; System.out.print(data); } }
7. The printf() Method
printf("I am %d years old", 25);
import java.io.PrintStream; class Main { public static void main(String[] args) { try { PrintStream output = new PrintStream("output.txt"); int age = 25; output.printf("I am %d years old.", age); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
8. print() Method With The PrintStream Class
import java.io.PrintStream; class Main { public static void main(String[] args) { String data = "This is a text inside the file."; try { PrintStream output = new PrintStream("output.txt"); output.print(data); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
9. Print Data With PrintStream
PrintStream output = new PrintStream("output.txt");
10. Print Data Using The printf() Method
PrintStream output = new PrintStream("output.txt");
Frequently Asked Questions
Printstream is basically an output stream in Java that gives you various methods so that you can print representations of various data points simultaneously. The most commonly used output stream in Java, System.out, is a Printstream. You can think of this as an instance of the Printstream class. It can also represent a standard output stream that can be used to print text output onto the console.
Another instance of the Printstream class is System.err. Printstream is also a byte outstream. It can convert all the output into bytes with the help of the specified encoding. This can be done using charset in the PrintStream() constructor invocation.
2. What is the use of PrintStream?
Printstream in Java is also not limited to a console. It can act as a filter output stream that can be connected to other output streams. It provides methods to print any data values or objects in an appropriate format. These methods will never throw an IOException when writing data to an underlying output.
- 2 ways to read a text file in Java? (solution)
- What is difference between BufferedReader and Scanner in Java (example)
- How to read an Excel file in Java? (solution)
- How to read a CSV file in Java? (example)
- How to create a file and directory in Java? (answer)
- How to read an XML file in Java? (answer)
- How to append text to an existing file in Java? (example)
- 5 Free Java 8 and Java 9 Courses for Programmers (courses)
- 5 Free Data Structure and Algorithm Courses (courses)
- 5 Free courses to learn Spring Core and Spring Boot for Java developers (courses)
- 10 tips to become a better Java developer (tips)
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.