Java UTF8 File Read readResourceUtf8(Class contextClass, String filename)

Here you can find the source of readResourceUtf8(Class contextClass, String filename)

Description

Loads a file (specified relative to the contextClass) as a string, assuming UTF-8 encoding.

License

Open Source License

Declaration

public static String readResourceUtf8(Class<?> contextClass, String filename) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import static com.google.common.io.Resources.getResource;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URL;

public class Main {
    /** Loads a file as a string, assuming UTF-8 encoding. */
    public static String readResourceUtf8(String filename) {
        return resourceToString(getResource(filename));
    }/*from  w ww  .j a v a2  s . c o  m*/

    /** Loads a file (specified relative to the contextClass) as a string, assuming UTF-8 encoding. */
    public static String readResourceUtf8(Class<?> contextClass, String filename) {
        return resourceToString(getResource(contextClass, filename));
    }

    private static String resourceToString(URL url) {
        try {
            return Resources.toString(url, UTF_8);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed to load resource: " + url, e);
        }
    }
}

Related

  1. openFileReaderUTF8(File file)
  2. readAllLines(File inputFile)
  3. readAsUTF8(String text)
  4. readLines(File inputFile)
  5. readRawUTF8Bytes(final byte[] bytes)
  6. readStringFromUTF8Stream(InputStream is)
  7. readStringUTF16LE(byte[] var0, int var1, int var2)
  8. readTextFile(File inputFile)
  9. readUTF8(DataInput buf)