Java Utililty Methods UTF8

List of utility methods to do UTF8

Description

The list of methods to do UTF8 are organized into topic(s).

Method

voidaddLineIds(String inFile, String outFile)
add Line Ids
StringBuilder builder = new StringBuilder();
List<String> list = Files.readAllLines(Paths.get(inFile), StandardCharsets.UTF_8);
int id = 1;
for (String line : list) {
    builder.append(id + "\t" + line);
    builder.append(System.lineSeparator());
    id++;
writeFile(builder.toString(), outFile, false);
voidappendToFile(String outputFile, String contents)
append To File
try {
    Files.write(Paths.get(outputFile), contents.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
    throw new UnsupportedOperationException("Failed to append to file '" + outputFile + "'", e);
StringasStringUTF8(byte[] bytes)
Return the String representation of the byte array.
if (bytes == null) {
    return null;
if (bytes.length == 0) {
    return EMPTY_STRING;
return new String(bytes, CHARSET_UTF8);
StringasUTF16BEEncoded(String basicString)
Take a basic PDF string and produce a string from its bytes as an UTF16-BE encoding.
try {
    return new String(asBytes(basicString), 2, basicString.length() - 2, "UTF-16BE");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("No UTF-16BE charset!");
StringasUtf8(byte[] bytes)
as Utf
return new String(bytes, Charset.forName("UTF-8"));
ReaderasUTF8(InputStream in)
Create a reader that uses UTF-8 encoding
return new InputStreamReader(in, utf8.newDecoder());
byte[]asUTF8bytes(String s)
as UT Fbytes
try {
    return s.getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
    throw new InternalError("UTF-8 not supported!");
InputStreamasUtf8ByteStream(final String string)
as Utf Byte Stream
final byte[] data = string.getBytes("utf-8");
final InputStream in = new ByteArrayInputStream(data);
return in;
StringasUTF8String(byte[] bytes)
Turns a byte[] array into a UTF8 string
return asString(bytes, UTF_8);
StringasUTFString(byte[] content)
as UTF String
return asUTFString(content, 0, content.length);