Java UTF File Write writeStringAsUTFByteArrayToDataOutput(final DataOutput out, final String str)

Here you can find the source of writeStringAsUTFByteArrayToDataOutput(final DataOutput out, final String str)

Description

Writes a string into a DataOutput.

License

BSD License

Parameter

Parameter Description
out The DataOutput into which the string will be written.
str The string to write.

Exception

Parameter Description
IOException an IO problem.

Declaration

public static void writeStringAsUTFByteArrayToDataOutput(final DataOutput out, final String str)
        throws IOException 

Method Source Code

//package com.java2s;
// Licensed under the terms of the New BSD License. Please see associated LICENSE file for terms.

import java.io.DataOutput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Main {
    /**//from  ww w .j a va 2 s .  c  o  m
     * Writes a string into a DataOutput. This method writes an integer, which indicates the size of a byte-array that
     * represents the string, followed by that byte array. The byte-array is the string, encoded in UTF-8. The method
     * {@link #readUTFByteArrayFromDataInput(DataInput)} can be used to read such a string from a DataInput.
     * 
     * @param out The DataOutput into which the string will be written.
     * @param str The string to write.
     * @throws IOException an IO problem.
     */
    public static void writeStringAsUTFByteArrayToDataOutput(final DataOutput out, final String str)
            throws IOException {
        byte[] byteArray = str.getBytes(StandardCharsets.UTF_8);
        out.writeInt(byteArray.length);
        out.write(byteArray);
    }
}

Related

  1. utf8Writer(File file)
  2. utf8Writer(final File f)
  3. writeFile(byte[] data, String outfile)
  4. writeFile(String fileName, ArrayList data, String outputFolder)
  5. writeFileUTF(String nom)
  6. writeToFile(File outputFile, String content)
  7. writeToFile(String outputFile, String contents)
  8. writeToFile(String outputFilePath, String generated)
  9. writeUTF8(DataOutput buf, String value)