Java Utililty Methods File Read by Charset

List of utility methods to do File Read by Charset

Description

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

Method

booleansetFileText(File file, Charset charset, String text)
Writes a string to a text file, using the specified encoding.
BufferedWriter bw = null;
boolean result = true;
try {
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
    bw.write(text, 0, text.length());
} catch (Exception e) {
    result = false;
} finally {
...
StringsetPropertiesVaule(File file, String key, String value, String comments, Charset charset)
Sets properties vaule.
if (file == null) {
    return null;
if (!file.exists()) {
    file.createNewFile();
if (charset == null) {
    charset = Charset.forName("ISO8859-1");
...
booleansetText(File file, Charset charset, String text)
Write a string to a text file, using the specified encoding.
BufferedWriter bw = null;
try {
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
    bw.write(text, 0, text.length());
    bw.flush();
    close(bw);
    return true;
} catch (Exception e) {
...
voidstringToFile(final String s, final File f, final Charset c)
string To File
byteArrayToFile(s.getBytes(c), f);
voidstringToFile(final String string, final Path path, final Charset charset)
Takes a string and writes it to a file.
try {
    if (Files.isRegularFile(path)) {
        Files.delete(path);
    Files.createDirectories(path.getParent());
    try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
        writer.write(string);
} catch (final IOException ex) {
    throw new RuntimeException(ex);
File[]toFiles(@Nonnull Process p, @Nonnull Charset charset)
to Files
List<File> result = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), charset))) {
    String line;
    while ((line = reader.readLine()) != null) {
        result.add(new File(line));
    try {
        p.waitFor();
...
StringtoString(File file, String charset)
to String
ByteBuffer buf = read(file, 0, (int) file.length());
return new String(buf.array(), buf.arrayOffset(), buf.remaining(), charset);
StringtoString(final Path filePath, final Charset encoding)
Read the contents of a file into String using specified encoding.
final List<String> lines = Files.readAllLines(filePath, encoding);
final StringBuilder sb = new StringBuilder();
for (String line : lines) {
    sb.append(line).append('\n');
return sb.toString();
CharsetDecoderutf8Decoder(CodingErrorAction codingErrorAction, Charset fileCharset)
utf Decoder
try {
    if (fileCharset == null)
        fileCharset = utf8Charset;
    if (codingErrorAction == null)
        codingErrorAction = CodingErrorAction.IGNORE;
    final CharsetDecoder encoder = fileCharset.newDecoder();
    encoder.reset();
    encoder.onUnmappableCharacter(codingErrorAction);
...