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

WriternewWriter(WritableByteChannel ch, Charset cs)
new Writer
if (ch == null)
    return null;
return Channels.newWriter(ch, cs.newEncoder(), -1);
WriteropenTextFileForWriting(File f, Charset encoding)
open Text File For Writing
FileOutputStream fos = new FileOutputStream(f);
byte[] header = getHeader(encoding);
if (!encoding.toString().equals("UTF-8")) {
    fos.write(header);
return new OutputStreamWriter(fos, encoding);
PrintWriteropenWriter(OutputStream stream, Charset charset, boolean autoflush)
Returns a PrintWriter attached to the stream with the the specified character set and autoflush.
return new PrintWriter(new java.io.BufferedWriter(new java.io.OutputStreamWriter(stream, charset)),
        autoflush);
voidwrite(char[] data, File file, String charsetName)
write
write(data, file, Charset.forName(charsetName));
voidwrite(File file, Charset charset, String content)
write
try (Writer writer = createWriter(file, charset)) {
    writer.write(content);
} catch (IOException e) {
    throw new IllegalStateException(e);
booleanwrite(File file, Object content, Charset cs, boolean append)
Writes string content into a file.
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
    osw = new OutputStreamWriter(new FileOutputStream(file, append), cs);
    bw = new BufferedWriter(osw);
    bw.write(content.toString());
    bw.flush();
} catch (FileNotFoundException e) {
...
voidwrite(final String s, final OutputStream out, Charset charset)
write
out.write(s.getBytes(charset));
voidwrite(Path file, CharBuffer data, Charset cs)
write
FileChannel fc = getFileChannel(file, "rw");
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, data.length());
mbb.put(cs.encode(data));
fc.close();
Pathwrite(Path path, Iterable lines, Charset cs, OpenOption... options)
write
return Files.write(path, lines, cs, options);
Pathwrite(Path path, Iterable lines, Charset cs, OpenOption... options)
write
try {
    return Files.write(path, lines, cs, options);
} catch (IOException e) {
    throw new RuntimeException(e);