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

InputStreambomStream(String charset, String file)
Convert stream to ByteArrayInputStream by given character set.
String localCharset = charset;
if (charset.equals("UTF-16") || charset.equals("UTF-32")) {
    localCharset += ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? "BE" : "LE";
if (!bom.containsKey(localCharset))
    throw new UnsupportedCharsetException("Charset:" + localCharset);
byte[] content = Files.readAllLines(Paths.get(file)).stream().collect(Collectors.joining())
        .getBytes(localCharset);
...
voidcopyFileWithEolConversion(File inFile, File outFile, Charset charset)
Copy file and create output directory if need.
File dir = outFile.getParentFile();
if (!dir.exists()) {
    dir.mkdirs();
String eol;
if (outFile.exists()) {
    eol = getEOL(outFile, charset);
} else {
...
CharsetdetectCharset(File f, String[] charsets)
detect Charset
Charset charset = null;
BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));
byte[] buffer = new byte[5120];
input.read(buffer);
for (String charsetName : charsets) {
    charset = detectCharset(buffer, Charset.forName(charsetName));
    if (charset != null) {
        break;
...
voiddoTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)
do Transe File Charset
System.out.println("convert file charset. Source file:" + srcFile.getAbsolutePath() + ". Dest file:"
        + destFile.getAbsolutePath());
String fileContent = getFileContent(srcFile, srcCharsetName);
if ("".equals(fileContent)) {
    copyFile(destFile, srcFile);
} else {
    saveFile(destFile, fileContent, destCharsetName);
StringfileContent(File file, Charset charset)
file Content
String fileContent;
try {
    fileContent = Files.toString(file, charset);
} catch (IOException e) {
    throw new IllegalStateException("Could not read " + file, e);
return fileContent;
StringfileToString(final File f, final Charset c)
file To String
return new String(fileToByteArray(f), c);
StringfileToString(final Path path, final Charset charset)
Reads a file and returns it as a string.
final StringBuilder sb = new StringBuilder();
try {
    if (Files.isRegularFile(path)) {
        try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
            final char[] buffer = new char[4096];
            int len;
            while ((len = reader.read(buffer, 0, buffer.length)) != -1) {
                sb.append(buffer, 0, len);
...
booleanfindPattern(File file, Charset charset, String patternString)
Search for a string pattern in a specific file
List<String> lines;
try {
    lines = Files.readLines(file, charset);
} catch (IOException e) {
    String fileName = file.getName();
    throw new IllegalStateException("Unable to execute rule \"S1451\" for file " + fileName, e);
if (lines == null) {
...
Stringfixture(String filename, Charset charset)
Reads fixture file from classpath, e.g src/test/resources and returns its content as string.
try {
    return Resources.toString(Resources.getResource(filename), charset);
} catch (IOException ex) {
    throw new IllegalArgumentException(ex);
StringgetContent(File file, String charsetName)
Gets the content of the given File .
if (!file.exists()) {
    throw new IOException(file.getAbsolutePath() + " doesn't exists.");
} else if (file.isDirectory()) {
    throw new IOException(file.getAbsolutePath() + " is a directory.");
} else if (!file.canRead()) {
    throw new IOException(file.getAbsolutePath() + " is not readable.");
int len = (int) file.length();
...