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

StringgetEOL(File file, Charset charset)
get EOL
String r = null;
try (BufferedReader in = Files.newBufferedReader(file.toPath(), charset)) {
    while (true) {
        int ch = in.read();
        if (ch < 0) {
            break;
        if (ch == '\n' || ch == '\r') {
...
StringgetFileContent(File file, String charsetName)
get File Content
if (file.isFile()) {
    FileInputStream inf = null;
    FileChannel inc = null;
    StringBuilder content = new StringBuilder();
    try {
        inf = new FileInputStream(file);
        inc = inf.getChannel();
        Charset charset = Charset.forName(charsetName);
...
StringgetFileContents(File file, String charset)
Reads the contents of a text file.
byte[] bytes = toByteArray(new FileInputStream(file), true);
return new String(bytes, charset);
CharsetgetFileEncodingCharset()
Returns the Charset which is equal to the "file.encoding" property.
try {
    return Charset.forName(System.getProperty("file.encoding"));
} catch (Throwable t) {
    return Charset.defaultCharset();
StringgetFileText(File file, Charset charset)
Reads a text file completely, using the specified encoding.
BufferedReader br = null;
try {
    if (!file.exists())
        return "";
    br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
    StringWriter sw = new StringWriter();
    int n;
    char[] cbuf = new char[1024];
...
intgetNumberOfNonEmptyLines(File file, Charset charset)
get Number Of Non Empty Lines
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
try {
    int count = 0;
    String line;
    while ((line = reader.readLine()) != null)
        if (!line.isEmpty())
            ++count;
    return count;
...
CharsetgetPatchFileCharset()
get Patch File Charset
return Charset.forName("ISO-8859-1");
StringgetPropertiesVaule(File file, String key, Charset charset)
Gets properties vaule.
if (file == null) {
    return null;
if (charset == null) {
    charset = Charset.forName("ISO8859-1");
Properties properties = new Properties();
FileInputStream fis = null;
...
StringgetString(File file, Charset charset)
Reads a file into a string.
FileInputStream stream = new FileInputStream(file);
try {
    FileChannel channel = stream.getChannel();
    try {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        if (charset == null)
            charset = Charset.defaultCharset();
        return charset.decode(buffer).toString();
...
ListgetUncommentedLines(File file, Charset forName)
get Uncommented Lines
try {
    List<String> ids = Lists.newArrayList();
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        for (String line; (line = br.readLine()) != null;) {
            if (!line.trim().startsWith("#") && !line.trim().isEmpty())
                ids.add(line.trim());
    return ids;
} catch (FileNotFoundException e) {
    throw new IOException(e);