PrintStream append(CharSequence, int, int) in Java

append(CharSequence, int, int): This method is available in the java.io.PrintStream class of Java.

Syntax:

PrintStream java.io.PrintStream.append(CharSequence csq, int start, int end)

This method takes three arguments. This method appends a subsequence of the specified character sequence to this output stream.

Parameters: Three parameters are required for this method.

csq: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".

start: The index of the first character in the subsequence.

end: The index of the character following the last character in the subsequence.

Returns: This output stream.

Throws:

1. IndexOutOfBoundsException - If the start or end is negative, the start is greater than the end, or the end is greater than csq.length().

Approach 1: When no exception

Java

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

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

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

        CharSequence csq = new String("HELLO JAVA PROGRAM");
        int start = 0, end = 10;
        printStream.append(csq, start, end);
        System.out.println("Successfully appended");

        printStream.close();
    }
}

Output:

Successfully appended


hello.txt

HELLO JAVA

Approach 2: IndexOutOfBoundsException 

Java

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

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

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

        CharSequence csq = new String("HELLO JAVA PROGRAM");
        int start = 0, end = 20;
        printStream.append(csq, start, end);
        System.out.println("Successfully appended");

        printStream.close();
    }
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 20, length 18 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.substring(String.java:1903) at java.base/java.lang.String.subSequence(String.java:1942) at java.base/java.io.PrintStream.append(PrintStream.java:1344) at com.PrintStream.PrintStreamappend3.main(PrintStreamappend3.java:15)


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