Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.io.PrintWriter class in Java | Set 2
Next article icon

Java.io.Printstream Class in Java | Set 2

Last Updated : 01 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Java.io.Printstream Class in Java | Set 1

More Methods:

  • PrintStream printf(Locale l, String format, Object… args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments.
    Syntax :public PrintStream printf(Locale l,
    String format,
    Object... args)
    Parameters:
    l - The locale to apply during formatting. If l is null then no localization is applied.
    format - A format string as described in Format string syntax
    args - Arguments referenced by the format specifiers in the format string.
    Returns:
    This output stream
    Throws:
    IllegalFormatException
    NullPointerException
    Java
    //Java program to demonstrate printf method import java.io.*; import java.util.Locale;  class PrintStreamDemo  {     public static void main(String[] args)      {         String s = "for";                  // create printstream object         PrintStream printStream = new PrintStream(System.out);                  // illustrating printf(Locale l, String format, Object... args) method         printStream.printf(Locale.US, "Geeks%sGeeks", s);     } } 
    Output:
    GeeksforGeeks
  • PrintStream printf(String format, Object… args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments.
    Syntax :public PrintStream printf(String format,
    Object... args)
    Parameters:
    format - A format string as described in Format string syntax
    args - Arguments referenced by the format specifiers in the format string.
    Returns:
    This output stream
    Throws:
    IllegalFormatException
    NullPointerException
    Java
    //Java program to demonstrate printf(String format, Object... args) method import java.io.*;  public class PrintStreamDemo  {     public static void main(String[] args)     {         String s = "for";                  // create printstream object         PrintStream obj= new PrintStream(System.out);                  // illustrating printf(String format, Object... args) method         obj.printf("Geeks%sGeeks", s);     } } 
    Output:
    GeeksforGeeks
  • void println(): Terminates the current line by writing the line separator string.
    Syntax :public void println()
    Java
    //Java program to demonstrate println() methods import java.io.PrintStream;  class PrintStreamDemo {     public static void main(String[] args)     {         PrintStream obj = new PrintStream(System.out);                  //illustrating println();         obj.println("GeeksforGeeks");     } } 
    Output:
    GeeksforGeeks
  • void println(boolean x): Prints a boolean and then terminate the line.
    Syntax :public void println(boolean x)
    Java
    //Java program to demonstrate println(boolean) method import java.io.*;  class PrintStreamDemo  {     public static void main(String[] args)      {         // create printstream object         PrintStream obj = new PrintStream(System.out);          //illustrating println(boolean) method         obj.println(true);          // flush the stream         obj.flush();     } } 
    Output:
    true
  • void println(char x): Prints a character and then terminate the line.
    Syntax :public void println(char x)
    Java
    //Java program to demonstrate println(char x) method import java.io.*;  public class PrintStreamDemo  {     public static void main(String[] args)      {         char c = 'g';                  // create printstream object         PrintStream obj = new PrintStream(System.out);                  // illustrating println(char x)         obj.println(c);                  // flush the stream         obj.flush();          } } 
    Output:
    g
  • void println(char[] x): Prints an array of characters and then terminate the line.
    Syntax :public void println(char[] x)
    Java
    //Java program to demonstrate println(char[] x) method import java.io.*;  public class PrintStreamDemo  {     public static void main(String[] args)      {         char[] c = {'G', 'E', 'E','K'};              // create printstream object         PrintStream obj = new PrintStream(System.out);              // illustrating println(char[] x)         obj.println(c);              // flush the stream         obj.flush();     } } 
    Output:
    GEEK
  • void println(double x): Prints a double and then terminate the line.
    Syntax :public void println(double x)
    Java
    //Java program to demonstrate println(double x) method import java.io.*;  public class PrintStreamDemo {     public static void main(String[] args)      {         double c = 5.42762;                  // create printstream object         PrintStream obj = new PrintStream(System.out);                  // illustrating println(double x)         obj.println(c);                  // flush the stream         obj.flush();     } } 
    Output:
    5.42762
  • void println(float x): Prints a float and then terminate the line.
    Syntax :public void println(float x)
    Java
    //Java program to demonstrate println(float x) method import java.io.*; public class PrintStreamDemo {     public static void main(String[] args)      {         float c = 5.168502f;                  // create printstream object         PrintStream obj = new PrintStream(System.out);              // illustrating println(float x)         obj.println(c);                  // flush the stream         obj.flush();     } } 
    Output:
    5.168502f
  • void println(int x): Prints an integer and then terminate the line.
    Syntax :public void println(boolean x)
    Java
    //Java program to demonstrate println(int x) method import java.io.*;  public class PrintStreamDemo {     public static void main(String[] args)     {                  int c = 5;                  // create printstream object         PrintStream obj = new PrintStream(System.out);                  // illustrating println(int x)         obj.println(c);                  // flush the stream         obj.flush();     } } 
    Output:
    5
  • void println(long x): Prints a long and then terminate the line.
    Syntax :public void println(long x)
    Java
    //Java program to demonstrate println(long x) method  import java.io.*; public class PrintStreamDemo  {     public static void main(String[] args)     {         long c = 123456789l;         try          {             // create printstream object             PrintStream obj= new PrintStream(System.out);                          // illustrating println(long x)             obj.println(c);                          // flush the stream             obj.flush();         }         catch (Exception ex)         {             ex.printStackTrace();         }     } } 
    Output:
    123456789
  • void println(Object x) :Prints an Object and then terminate the line.
    Syntax :public void println(Object x)
    Java
    //Java program to demonstrate println(Object x) method import java.io.*;  public class PrintStreamDemo  {          public static void main(String[] args)      {         // create printstream object         PrintStream obj = new PrintStream(System.out);                  //illustrating println(Object X)         obj.println(obj);                  // flush the stream         obj.flush();     } } 
    Output:
    java.io.PrintStream@15db9742
  • void println(String x) :Prints a String and then terminate the line.
    Syntax :public void println(boolean x)
    Java
    import java.io.*; //Java program to demonstrate println(String x) method public class PrintStreamDemo  {     public static void main(String[] args)      {         String c = "GeeksforGeeks";                  // create printstream object         PrintStream ps = new PrintStream(System.out);                  // illustrating println(String x)         ps.println(c);                  // flush the stream         ps.flush();     } } 
    Output:
    GeeksforGeeks
  • protected void setError() :Sets the error state of the stream to true.
    Syntax :public void println(String x)
    Java
    //Java program to demonstrate setError() method import java.io.*;  public class PrintStreamDemo extends PrintStream  {     public PrintStreamDemo(OutputStream out)      {         super(out);     }     public static void main(String[] args)      {         byte c[] = {65, 66, 67, 68, 69, 70, 71};                  // create printstream object         PrintStreamDemo obj = new PrintStreamDemo(System.out);                  // illustrating write() method         obj.write(c, 1, 3);                  // flush the stream         obj.flush();                  //illustrating setError() method          obj.setError();     } } 
    Output:
    BCD
  • void write(byte[] buf, int off, int len) :Writes len bytes from the specified byte array starting at offset off to this stream.
    Syntax :public void write(byte[] buf,
    int off,
    int len)
    Overrides:
    write in class FilterOutputStream
    Parameters:
    buf - A byte array
    off - Offset from which to start taking bytes
    len - Number of bytes to write
    Java
    //Java program to demonstrate write(int b) method import java.io.*;  public class PrintStreamDemo  {     public static void main(String[] args)      {         byte c = 65;              // create printstream object         PrintStream obj = new PrintStream(System.out);                  //illustrating write(int b)         obj.write(c);              // flush the stream         obj.flush();          } } 
    Output:
    BCD
  • void write(int b) :Writes the specified byte to this stream.
    Syntax :public void write(int b)
    Overrides:
    write in class FilterOutputStream
    Parameters:
    b - The byte to be written
    Java
    //Java program to demonstrate write(int b) method import java.io.*;  public class PrintStreamDemo  {     public static void main(String[] args)      {         byte c = 65;                  // create printstream object         PrintStream obj = new PrintStream(System.out);                  //illustrating write(int b)         obj.write(c);                  // flush the stream         obj.flush();          } } 
    Output:
    A


Next Article
Java.io.PrintWriter class in Java | Set 2

N

Nishant Sharma
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

  • Java.io.Printstream Class in Java | Set 1
    A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the c
    5 min read
  • Java.io.PrintWriter class in Java | Set 2
    Java.io.PrintWriter class in Java | Set 1 More methods: PrintWriter printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this writer using the specified format string and arguments. Syntax :public PrintWriter printf(Locale l, String format, Object...
    7 min read
  • Java.io.ObjectInputStream Class in Java | Set 2
    Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStrea
    6 min read
  • Java.io.PrintWriter class in Java | Set 1
    Java PrintWriter class gives Prints formatted representations of objects to a text-output stream. It implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. Unlike the PrintStream class, if au
    5 min read
  • Java.io.ObjectInputStream Class in Java | Set 1
    ObjectInputStream Class deserializes the primitive data and objects previously written by ObjectOutputStream. Both ObjectOutputStream and ObjectInputStream are used as it provides storage for graphs of object.It ensures that the object it is working for, matches the classes of JVM i.e Java Virtual M
    9 min read
  • Java.io.PipedInputStream class in Java
    Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination. PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads
    5 min read
  • Java.io.OutputStream class in Java
    This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo
    2 min read
  • Java.io.ObjectOutputStream Class in Java | Set 1
    An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. Only objects that support the java.io.Serializable in
    9 min read
  • Java.io.PushbackInputStream class in Java
    Pushback is used on an input stream to allow a byte to be read and then returned (i.e, "pushed back") to the stream. The PushbackInputStream class implements this idea. It provides a mechanism "peek" at what is coming from an input stream without disrupting it. It extends FilterInputStream.Fields: p
    7 min read
  • Java.io.StringReader class in Java
    StringReader class in Java is a character stream class whose source is a string. It inherits Reader Class. Closing the StringReader is not necessary, it is because system resources like network sockets and files are not used. Let us check more points about StringReader Class in Java. Declare StringR
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences