Java Utililty Methods UTF File Write

List of utility methods to do UTF File Write

Description

The list of methods to do UTF File Write are organized into topic(s).

Method

PrintWriterwriteFileUTF(String nom)
write File UTF
return new PrintWriter(getUTF8Writer(new File(nom)));
voidwriteStringAsUTFByteArrayToDataOutput(final DataOutput out, final String str)
Writes a string into a DataOutput.
byte[] byteArray = str.getBytes(StandardCharsets.UTF_8);
out.writeInt(byteArray.length);
out.write(byteArray);
voidwriteToFile(File outputFile, String content)
Writes string content to a file.
Preconditions.checkNotNull(outputFile);
Preconditions.checkNotNull(content);
try (BufferedWriter outWriter = Files.newWriter(outputFile, StandardCharsets.UTF_8)) {
    outWriter.write(content);
voidwriteToFile(String outputFile, String contents)
write To File
writeToFile(Paths.get(outputFile), contents);
voidwriteToFile(String outputFilePath, String generated)
write To File
File outputFile = new File(outputFilePath);
if (outputFile.getParent() != null && !new File(outputFile.getParent()).exists()) {
    File parent = new File(outputFile.getParent());
    parent.mkdirs();
try (PrintWriter output = new PrintWriter(outputFile, Charset.forName("UTF-8").name())) {
    output.print(generated);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
...
voidwriteUTF8(DataOutput buf, String value)
Writes an UTF8 string to a byte buffer.
final byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
if (bytes.length >= Short.MAX_VALUE) {
    throw new IOException(
            "Attempt to write a string with a length greater than Short.MAX_VALUE to ByteBuf!");
writeVarInt(buf, bytes.length);
buf.write(bytes);
voidwriteUTF8(final String filePath, final String content)
write UTF
Files.write(FileSystems.getDefault().getPath(filePath), content.getBytes(StandardCharsets.UTF_8));
voidwriteUTF8(OutputStream out, String str)
Writes the string out as UTF-8
byte[] s = str.getBytes(UTF8);
out.write(s);
voidwriteUtf8String(final DataOutputStream out, final String str)
Writes a UTF-8 string to the buffer.
byte[] bytes = str.getBytes(CHARSET_UTF8);
if (bytes.length > MAXLEN) {
    throw new IllegalArgumentException("Encoded UTF-8 string too long.");
out.writeShort(bytes.length);
out.write(bytes);
voidwriteUTF8WithLength(OutputStream out, String str)
Writes out a 4 byte integer of the length (in bytes!) of the String, followed by the String (as UTF-8)
byte[] s = str.getBytes(UTF8);
writeInt4(out, s.length);
out.write(s);