Java Utililty Methods Text File Write nio

List of utility methods to do Text File Write nio

Description

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

Method

voidwriteString(DataOutput dOut, String value)
Writes out value to dOut.
if (value == null) {
    dOut.writeUTF(value);
    return;
byte[] data = value.getBytes(StandardCharsets.UTF_8.name());
if (data.length > CONVERSION_TRESHOLD) {
    dOut.writeUTF(DATA_VERSION);
    dOut.writeInt(data.length);
...
voidwriteString(File file, String content)
write String
writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
voidwriteString(final File file, final String str)
Writes a string to a file.
writeString(new FileOutputStream(file), str);
voidwriteString(OutputStream buffer, String s)
write String
if (s == null || s.isEmpty()) {
    writeCompactInt(buffer, 0);
    return;
s += '\0';
boolean def = defaultEncoder.canEncode(s);
byte[] bytes = s.getBytes(def ? defaultCharset : utf16leCharset);
writeCompactInt(buffer, def ? bytes.length : -bytes.length / 2);
...
voidwriteStringToFile(File file, String content, String value)
write String To File
Files.write(file.toPath(), Collections.singletonList(new String(content.getBytes(value), value)),
        Charset.forName(value), StandardOpenOption.CREATE);
voidwriteStringToFile(File file, String string)
write String To File
Files.write(file.toPath(), string.getBytes(TEXT_CHARSET));
voidwriteStringToFile(File file, String text)
write the given String to a the given file
Files.write(Paths.get(file.toURI()), text.getBytes());
voidwriteStringToFile(final File file, final String content)
write String To File
if (content == null) {
    throw new UnsupportedOperationException("Cannot write null to file. Consider deleting it instead");
try (OutputStream fos = openOutputStream(file)) {
    fos.write(content.getBytes(Charset.forName("UTF-8")));
voidwriteStringToFile(String s, File f)
Writes a string into a file, using UTF-8 encoding
BufferedWriter w = null;
try {
    w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), Charset.forName("UTF-8")));
    w.append(s);
} finally {
    if (w != null) {
        w.close();
voidwriteTextFile(final File file, final String text)
write Text File
final OutputStream stream = new FileOutputStream(file);
final BufferedOutputStream output = new BufferedOutputStream(stream);
final OutputStreamWriter writer = new OutputStreamWriter(output, UTF_8);
writer.write(text);
writer.flush();
writer.close();