println(): This method is available in the java.io.PrintWriter class.
1. println()
void java.io.PrintWriter.println()
This method terminates the current line by writing the line separator string. The line separator is System.lineSeparator() and is not necessarily a single newline character ('\n').
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);printWriter.println();System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
2. println(boolean x)
Syntax:
void java.io.PrintWriter.println(boolean x)
Prints a boolean value and then terminates the line.
Parameters: x the boolean value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);boolean x = false;printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
3. println(char x)
Syntax:
void java.io.PrintWriter.println(char x)
Prints a character and then terminates the line.
Parameters: x the char value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);char x = 'A';printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
4. println(char[] x)
Syntax:
void java.io.PrintWriter.println(char[] x)
Prints an array of characters and then terminates the line.
Parameters: x the array of char values to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);char x[] = { 'A', 'B', 'C', 'D' };printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
5. println(double x)
Syntax:
void java.io.PrintWriter.println(double x)
Prints a double-precision floating-point number and then terminates the line.
Parameters: x the double value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);double x = 1000.999d;printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
6. println(float x)
Syntax:
void java.io.PrintWriter.println(float x)
Prints a floating-point number and then terminates the line.
Parameters: x the float value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);float x = 1000.99f;printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
7. println(int x)
Syntax:
void java.io.PrintWriter.println(int x)
Prints an integer and then terminates the line.
Parameters: x the int value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);int x = 1000;printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
8. println(long x)
Syntax:
void java.io.PrintWriter.println(long x)
Prints a long integer and then terminates the line.
Parameters: x the long value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);long x = 1000999L;printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
9. println(Object x)
Syntax:
void java.io.PrintWriter.println(Object x)
Prints an Object and then terminates the line.
Parameters: x The Object to be printed.
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);Object x = 11991;printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
10. println(String x)
Syntax:
void java.io.PrintWriter.println(String x)
Prints a String and then terminates the line.
Parameters: x the String value to be printed
Approach
Java
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class PrintWriterprintln {public static void main(String[] args)throws FileNotFoundException {File file = new File("D:\\hello.txt");PrintWriter printWriter = new PrintWriter(file);String x = "HELLO";printWriter.println(x);System.out.println("Printing done");printWriter.close();}}
Output:
Printing done
Some other methods of PrintWriter
append(char): This method appends the specified character to this writer.
append(CharSequence): This method appends the specified character sequence to this writer.
append(CharSequence, int, int): This method appends a subsequence of the specified character sequence to this writer.
checkError(): This method flushes the stream if it's not closed and checks its error state.
close(): This method closes the stream and releases any system resources associated with it.
flush(): This method flushes the stream.
format(String, Object...): This method writes a formatted string to this writer using the specified format string and arguments.
format(Locale, String, Object...): This method writes a formatted string to this writer using the specified format string and arguments.
print(boolean): This method prints a boolean value.
printf(String, Object...): It is a convenient method to write a formatted string to this writer using the specified format string and arguments.
printf(Locale, String, Object...): It is a convenient method to write a formatted string to this writer using the specified format string and arguments.
println(): This method terminates the current line by writing the line separator string.
PrintWriter(File): This method creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriter(Writer, boolean): This method creates a new PrintWriter.
PrintWriter(OutputStream, boolean, Charset): This method creates a new PrintWriter from an existing OutputStream.
PrintWriter(OutputStream): This method creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
PrintWriter(String): This method creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriter(Writer): This method creates a new PrintWriter, without automatic line flushing.
PrintWriter(File, Charset): This method creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter(File, String): This method creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter(OutputStream, boolean): This method creates a new PrintWriter from an existing OutputStream.
PrintWriter(String, Charset): This method creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriter(String, String): This method creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
write(char[]): This method writes an array of characters.
write(int): This method writes a single character.
write(String): This method writes a string
write(char[], int, int): This method writes A Portion of an array of characters.
write(String, int, int): This method writes a portion of a string.
No comments:
Post a Comment