DataOutputStream writeBytes(String) in Java

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

Syntax:

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

This method takes one argument. This method writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits.

Note:

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

Parameters: One parameter is required for this method.

s: a string of bytes 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 DataOutputStreamwriteBytes {
    public static void main(String[] args) throws IOException {

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

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

    }
}

Output:

Successfully writes


hello.txt

hello


No comments:

Post a Comment