List of utility methods to do Text File Write by Charset
Writer | newWriter(WritableByteChannel ch, Charset cs) new Writer if (ch == null) return null; return Channels.newWriter(ch, cs.newEncoder(), -1); |
Writer | openTextFileForWriting(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); |
PrintWriter | openWriter(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); |
void | write(char[] data, File file, String charsetName) write write(data, file, Charset.forName(charsetName)); |
void | write(File file, Charset charset, String content) write try (Writer writer = createWriter(file, charset)) { writer.write(content); } catch (IOException e) { throw new IllegalStateException(e); |
boolean | write(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) { ... |
void | write(final String s, final OutputStream out, Charset charset) write out.write(s.getBytes(charset)); |
void | write(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();
|
Path | write(Path path, Iterable extends CharSequence> lines, Charset cs, OpenOption... options) write return Files.write(path, lines, cs, options);
|
Path | write(Path path, Iterable extends CharSequence> lines, Charset cs, OpenOption... options) write try { return Files.write(path, lines, cs, options); } catch (IOException e) { throw new RuntimeException(e); |