Java.io.Printstream Class in Java | Set 2
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
import java.io.*;
import java.util.Locale;
class PrintStreamDemo
{
public static void main(String[] args)
{
String s = "for" ;
PrintStream printStream = new PrintStream(System.out);
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
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
String s = "for" ;
PrintStream obj= new PrintStream(System.out);
obj.printf( "Geeks%sGeeks" , s);
}
}
|
Output:
GeeksforGeeks
- void println(): Terminates the current line by writing the line separator string.
Syntax :public void println()
import java.io.PrintStream;
class PrintStreamDemo
{
public static void main(String[] args)
{
PrintStream obj = new PrintStream(System.out);
obj.println( "GeeksforGeeks" );
}
}
|
Output:
GeeksforGeeks
- void println(boolean x): Prints a boolean and then terminate the line.
Syntax :public void println(boolean x)
import java.io.*;
class PrintStreamDemo
{
public static void main(String[] args)
{
PrintStream obj = new PrintStream(System.out);
obj.println( true );
obj.flush();
}
}
|
Output:
true
- void println(char x): Prints a character and then terminate the line.
Syntax :public void println(char x)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
char c = 'g' ;
PrintStream obj = new PrintStream(System.out);
obj.println(c);
obj.flush();
}
}
|
Output:
g
- void println(char[] x): Prints an array of characters and then terminate the line.
Syntax :public void println(char[] x)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
char [] c = { 'G' , 'E' , 'E' , 'K' };
PrintStream obj = new PrintStream(System.out);
obj.println(c);
obj.flush();
}
}
|
Output:
GEEK
- void println(double x): Prints a double and then terminate the line.
Syntax :public void println(double x)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
double c = 5.42762 ;
PrintStream obj = new PrintStream(System.out);
obj.println(c);
obj.flush();
}
}
|
Output:
5.42762
- void println(float x): Prints a float and then terminate the line.
Syntax :public void println(float x)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
float c = 5 .168502f;
PrintStream obj = new PrintStream(System.out);
obj.println(c);
obj.flush();
}
}
|
Output:
5.168502f
- void println(int x): Prints an integer and then terminate the line.
Syntax :public void println(boolean x)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
int c = 5 ;
PrintStream obj = new PrintStream(System.out);
obj.println(c);
obj.flush();
}
}
|
Output:
5
- void println(long x): Prints a long and then terminate the line.
Syntax :public void println(long x)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
long c = 123456789l;
try
{
PrintStream obj= new PrintStream(System.out);
obj.println(c);
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)
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
PrintStream obj = new PrintStream(System.out);
obj.println(obj);
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)
import java.io.*;
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
String c = "GeeksforGeeks" ;
PrintStream ps = new PrintStream(System.out);
ps.println(c);
ps.flush();
}
}
|
Output:
GeeksforGeeks
- protected void setError() :Sets the error state of the stream to true.
Syntax :public void println(String x)
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 };
PrintStreamDemo obj = new PrintStreamDemo(System.out);
obj.write(c, 1 , 3 );
obj.flush();
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
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
byte c = 65 ;
PrintStream obj = new PrintStream(System.out);
obj.write(c);
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
import java.io.*;
public class PrintStreamDemo
{
public static void main(String[] args)
{
byte c = 65 ;
PrintStream obj = new PrintStream(System.out);
obj.write(c);
obj.flush();
}
}
|
Output:
A
Last Updated :
12 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...