PrintStream println() in Java

println(): This method is available in the java.io.PrintStream class of Java.

1. println()

Syntax:

void java.io.PrintStream.println()

This method terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator and is not necessarily a single newline character ('\n').

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        printStream.println();

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


2. println(boolean x)

Syntax:

void java.io.PrintStream.println(boolean x)

This method takes one argument. This method prints a boolean and then terminates the line. This method behaves as though it invokes print(boolean) and then println().

Parameters: x The boolean to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        boolean x = true;
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


3. println(char x)

Syntax:

void java.io.PrintStream.println(char x)

Prints a character and then terminates the line. This method behaves as though it invokes print(char) and then println().

Parameters: x The char to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        char x = 'A';
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done

4. println(char[] x)

Syntax:

void java.io.PrintStream.println(char[] x)

Prints an array of characters and then terminates the line. This method behaves as though it invokes print(char []) and then println().

Parameters: x an array of chars to print.

Approach

Java


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        char x[] = { 'A', 'B', 'C', 'D' };
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


5. println(double x)

Syntax:

void java.io.PrintStream.println(double x)

Prints a double and then terminate the line. This method behaves as though it invokes print(double) and then println(). Parameters:x The double to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        double x = 1000.00091d;
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


6. println(float x)

Syntax:

void java.io.PrintStream.println(float x)

Prints a float and then terminates the line. This method behaves as though it invokes print(float) and then println().

Parameters: x The float to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        float x = 1000.07f;
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


7. println(int x)

Syntax:

void java.io.PrintStream.println(int x)

Prints an integer and then terminate the line. This method behaves as though it invokes print(int) and then println().

Parameters: x The int to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        int x = 1000;
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


8. println(long x)

Syntax:

void java.io.PrintStream.println(long x)

Prints a long and then terminate the line. This method behaves as though it invokes print(long) and then println().

Parameters: x a The long to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        long x = 10001881L;
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


9. println(Object x)

Syntax:

void java.io.PrintStream.println(Object x)

Prints an Object and then terminates the line. This method calls first String.valueOf(x) to get the printed object's string value, then behave as though it invokes print(String) and then println().

Parameters: x The Object to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        Object x = 10001;
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


10. println(String x)

Syntax:

void java.io.PrintStream.println(String x)

Prints a String and then terminates the line. This method behaves as though it invokes print(String) and then println().

Parameters: x The String to be printed.

Approach

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class PrintStreamprintln {
    public static void main(String[] args)
throws FileNotFoundException {

        File file = new File("D:\\hello.txt");
        PrintStream printStream = new PrintStream(file);

        String x = "HELLO JAVA";
        printStream.println(x);

        System.out.println("Printing done");
        printStream.close();
    }
}

Output:

Printing done


Some other methods of PrintStream

append(char)This method appends the specified character to this output stream.

append(CharSequence)This method appends the specified character sequence to this output stream.

append(CharSequence, int, int)This method appends a subsequence of the specified character sequence to this output stream.

checkError()This method flushes the stream and checks its error state.

close()This is done by flushing the stream and then closing the underlying output stream.

flush()This method flushes the stream.

format(String, Object...)This method writes a formatted string to this output stream using the specified format string and arguments.

format(Locale, String, Object...)This method writes a formatted string to this output stream 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 output stream using the specified format string and arguments.

printf(Locale, String, Object...)It is a convenient method to write a formatted string to this output stream using the specified format string and arguments.

println()This method terminates the current line by writing the line separator string.

PrintStream(File)This method creates a new print stream, without automatic line flushing, with the specified file.

PrintStream(OutputStream, boolean, String)This method creates a new print stream, with the specified OutputStream, line flushing, and character encoding.

PrintStream(OutputStream)This method creates a new print stream, without automatic line flushing, with the specified OutputStream.

PrintStream(String)This method creates a new print stream, without automatic line flushing, with the specified file name.

PrintStream(File, Charset)This method creates a new print stream, without automatic line flushing, with the specified file and charset.

PrintStream(File, String)This method creates a new print stream, without automatic line flushing, with the specified file and charset.

PrintStream(OutputStream, boolean)This method creates a new print stream, with the specified OutputStream and line flushing.

PrintStream(String, Charset)This method creates a new print stream, without automatic line flushing, with the specified file name and charset.

PrintStream(String, String)This method creates a new print stream, without automatic line flushing, with the specified file name and charset.

PrintStream(OutputStream, boolean, Charset)This method creates a new print stream, with the specified OutputStream, line flushing, and charset.

write(byte[]) This method writes all bytes from the specified byte array to this stream.

write(int)This method writes the specified byte to this stream.

write(byte[], int, int)This method writes len bytes from the specified byte array starting to offset off to this stream.

writeBytes(byte[])This method writes all bytes from the specified byte array to this stream.

No comments:

Post a Comment