DataOutputStream writeChars(String) in Java

writeChars(String): This method is available in the java.io.DataOutputStream class of Java.

Syntax:

void java.io.DataOutputStream.writeChars(String s) throws IOException

This method takes one argument. This method writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by the writeChar method.

Note: If no exception is thrown, the counter written is incremented by twice the length of s.

Parameters: One parameter is required for this method.

s: a String value to be written.

Returns: NA

Throws:

IOException - if an I/O error occurs.

Approach

Java

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamwriteChars {
    public static void main(String[] args) throws IOException {

        FileOutputStream file =
new FileOutputStream("D:\\hello.txt");
        DataOutputStream data = new DataOutputStream(file);

        String s = "hello";
        data.writeChars(s);
        System.out.println("Successfully writes");
        data.close();

    }
}

Output:

Successfully writes


hello.txt

 h e l l o


No comments:

Post a Comment