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

voidwriter(String path, String content, String charset)
writer
OutputStream out = fileOut(path, false);
out.write(content.getBytes(charset));
close(out);
BufferedWriterwriterBuffered(final File file, final Charset charset)
Open a buffered Writer , equivalent to:
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
return writer;
StringwriteStreamToString(InputStream is, String charsetName)
write Stream To String
return writeStreamToString(is, Charset.forName(charsetName));
voidwriteString(OutputStream os, String string, CharsetEncoder encoder)
write String
OutputStreamWriter writer = new OutputStreamWriter(os, encoder);
writer.write(string);
writer.flush();
voidwriteString(OutputStream out, String charset, String value)
Convert a \n string to a platform encoding.
writeString(out, Charset.forName(charset), value);
voidwriteStringToFile(File file, String s, String charset)
Writes string to file.
writeStringToFile(file, s, charset, false);
voidwriteStringToFile(String fileContent, String filePath, String charset)
write String To File
File cacheFile = new File(filePath);
File fileContents;
if (cacheFile.exists()) {
    try {
        cacheFile.delete();
    } catch (Exception var25) {
        ;
} else {
    fileContents = cacheFile.getParentFile();
    if (!fileContents.exists()) {
        fileContents.mkdirs();
try {
    cacheFile.createNewFile();
} catch (Exception var24) {
    ;
cacheFile = new File(filePath);
if (cacheFile.exists()) {
    byte[] fileContentsBytes;
    if (null != charset && !charset.trim().isEmpty()) {
        fileContentsBytes = fileContent.getBytes(charset);
    } else {
        fileContentsBytes = fileContent.getBytes();
    ByteBuffer buf = ByteBuffer.wrap(fileContentsBytes);
    FileOutputStream fout = null;
    FileChannel outc = null;
    try {
        fout = new FileOutputStream(filePath);
        outc = fout.getChannel();
        outc.write(buf);
    } catch (FileNotFoundException var22) {
        throw var22;
    } catch (IOException var23) {
        throw var23;
    } finally {
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException var21) {
                ;
        if (outc != null) {
            try {
                outc.close();
            } catch (IOException var20) {
                ;
voidwriteStringToFile(String filename, String contents, Charset encoding)
write String To File
try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), encoding)) {
    out.write(contents);
voidwriteStringToFile(String fileName, String str, String charset)
Writes a string to a file, replacing any existing contents, using the platform's default character set.
File file = new File(fileName);
if (!Charset.isSupported(charset)) {
    throw new UnsupportedEncodingException(charset);
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), charset)) {
    writer.write(str);
voidwriteStringToStream(String string, OutputStream stream, Charset charset)
Writes given string to given stream, using given charset
OutputStreamWriter osw = new OutputStreamWriter(stream, charset);
try {
    osw.append(string);
    osw.flush();
} finally {
    osw.close();