Java Utililty Methods Text File Write by Charset

List of utility methods to do Text File Write by Charset

Description

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

Method

booleanwrite(String toWrite, File file, Charset charset)
write
FileLock lock = null;
FileChannel fileChannel = null;
try {
    if (!file.exists() && !file.createNewFile()) {
        return false;
    fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
    ByteBuffer buffer = ByteBuffer.wrap(toWrite.getBytes(charset));
...
voidwriteAll(Path path, String data, Charset cs)
write All
try (OutputStream os = Files.newOutputStream(path, StandardOpenOption.CREATE,
        StandardOpenOption.TRUNCATE_EXISTING)) {
    os.write(data.getBytes(ENCODING_UTF8));
voidwriteAll(String data, OutputStream outputStream, Charset charset)
Write string to byte stream
writeAll(data, outputStream, charset.name());
voidwriteContent(String content, String charset, File file)
write Content
if (file.isDirectory()) {
    throw new IOException("Cannot write content to directory");
if (file.exists()) {
    file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(content.getBytes(charset));
...
booleanwriteFile(File file, String content, Charset charset)
write File
try {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
    bw.write(content);
    bw.flush();
    bw.close();
    return true;
} catch (IOException e) {
    return false;
...
voidwriteFile(String content, File file, Charset encoding)
write File
try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding))) {
    out.write(content);
BooleanwriteFile(String path, String content, Charset charset)
write File
Writer writer = null;
try {
    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), charset));
    if (content != null) {
        writer.write(content);
} catch (IOException e) {
    return false;
...
voidwriteFile(String path, String content, Charset encoding)
write text to file
Files.write(Paths.get(path), content.getBytes(encoding), StandardOpenOption.CREATE);
voidwriteFile(String path, String output, Charset encoding)
write File
Files.write(Paths.get(path), output.getBytes(encoding));
OutputStreamWriterwriter(OutputStream out, String charset)
writer
OutputStreamWriter result;
if (charset == null) {
    result = writer(out);
} else {
    result = writer(out, Charset.forName(charset));
return result;