DataOutputStream writeUTF(String) in Java

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

Syntax:

void java.io.DataOutputStream.writeUTF(String str) throws IOException

This method takes one argument. This method writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

Parameters: One parameter is required for this method.

str: a string to be written.

Returns: NA

Throws:

1. UTFDataFormatException - if the modified UTF-8 encoding of str would exceed 65535 bytes in length.

2. IOException - if some other I/O error occurs.

Approach

Java

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

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

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

        String str = "UTF-8";
        data.writeUTF(str);
        System.out.println("Successfully writes");
        data.close();

    }
}

Output:

Successfully writes


No comments:

Post a Comment