PrintWriter write(String, int, int) in Java

write(String, int, int): This method is available in the java.io.PrintWriter class of Java.

Syntax:

void java.io.PrintWriter.write(String s, int off, int len)

This method takes three arguments. This method writes a portion of a string.

Parameters: Three parameters are required for this method.

s: A String.

off: Offset from which to start writing characters.

len: Number of characters to write.

Returns: NA

Throws:

1. IndexOutOfBoundsException - If the values of the off and len parameters cause the corresponding method of the underlying Writer to throw an IndexOutOfBoundsException.

2. NullPointerException- if str is null.

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintWriterwrite5 {
    public static void main(String[] args)
throws FileNotFoundException {
        File file = new File("D:\\hello.txt");
        PrintWriter printWriter = new PrintWriter(file);

        String str = "HELLO JAVA";
        int off = 0, len = 3;
        printWriter.write(str, off, len);

        System.out.println("Successfully writes");
        printWriter.close();
    }
}

Output:

Successfully writes


Approach 2: IndexOutOfBoundsException 

Java

package com.PrintWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintWriterwrite5 {
    public static void main(String[] args)
throws FileNotFoundException {
        File file = new File("D:\\hello.txt");
        PrintWriter printWriter = new PrintWriter(file);

        String str = "HELLO JAVA";
        int off = 0, len = 13;
        printWriter.write(str, off, len);

        System.out.println("Successfully writes");
        printWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 13, length 10 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.getChars(String.java:873) at java.base/java.io.BufferedWriter.write(BufferedWriter.java:229) at java.base/java.io.PrintWriter.write(PrintWriter.java:542) at com.PrintWriter.PrintWriterwrite5.main(PrintWriterwrite5.java:14)


Approach 3: NullPointerException

Java

package com.PrintWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PrintWriterwrite5 {
    public static void main(String[] args)
throws FileNotFoundException {
        File file = new File("D:\\hello.txt");
        PrintWriter printWriter = new PrintWriter(file);

        String str = null;
        int off = 0, len = 3;
        printWriter.write(str, off, len);

        System.out.println("Successfully writes");
        printWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.getChars(int, int, char[], int)" because "s" is null at java.base/java.io.BufferedWriter.write(BufferedWriter.java:229) at java.base/java.io.PrintWriter.write(PrintWriter.java:542) at com.PrintWriter.PrintWriterwrite5.main(PrintWriterwrite5.java:14)


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