Example usage for com.google.common.io Files toString

List of usage examples for com.google.common.io Files toString

Introduction

In this page you can find the example usage for com.google.common.io Files toString.

Prototype

public static String toString(File file, Charset charset) throws IOException 

Source Link

Usage

From source file:com.google.devtools.build.lib.skylark.util.SkylarkUtil.java

private static void copyExistingSkylarkFiles(Scratch scratch, String from, String to) throws IOException {
    File rulesDir = new File(from);
    if (rulesDir.exists() && rulesDir.isDirectory()) {
        for (String fileName : rulesDir.list()) {
            File file = new File(from + "/" + fileName);
            if (file.isFile() && fileName.endsWith(".bzl")) {
                String context = Files.toString(file, Charset.defaultCharset());
                Path path = scratch.resolve(to + "/" + fileName);
                if (path.exists()) {
                    scratch.overwriteFile(path.getPathString(), context);
                } else {
                    scratch.file(path.getPathString(), context);
                }/*  w  w  w. ja  v a  2  s  .  c  om*/
            }
        }
    }
}

From source file:com.github.enr.markdownj.extras.FileUtils.java

/**
 * /*from ww  w  .  j  av  a 2s  . c  om*/
 * @param filename
 * @param encoding the encoding to use, null means platform default
 * @return the given file content
 */
public static String readFileFromPath(String filename, String encoding) {
    try {
        Charset charset = charsetForNameOrDefault(encoding);
        return Files.toString(new File(filename), charset);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openqa.selenium.atoms.JavaScriptLoader.java

static String loadResource(String resourcePath, String resourceTask) throws IOException {
    URL resourceUrl = JavaScriptLoader.class.getResource(resourcePath);
    if (resourceUrl != null) {
        return Resources.toString(resourceUrl, Charsets.UTF_8);
    } else {//from w  w  w  .  j  a va  2 s . co m
        new Build().of(resourceTask).go();

        File topDir = InProject.locate("Rakefile").getParentFile();
        File builtFile = new File(topDir, taskToBuildOutput(resourceTask));
        return Files.toString(builtFile, Charsets.UTF_8);
    }
}

From source file:org.apache.drill.common.util.DrillFileUtils.java

public static String getResourceAsString(String fileName) throws IOException {
    return Files.toString(getResourceAsFile(fileName), Charsets.UTF_8);
}

From source file:brooklyn.networking.tunnelling.SshTunnelling.java

public static String generateRsaKey(SshMachineLocation machine, String privateKeyFile) {
    String publicKeyFile = (privateKeyFile.startsWith("~/") ? privateKeyFile.substring(2) : privateKeyFile)
            + ".pub";
    try {/*from ww  w  . j  a v  a 2  s.c  o  m*/
        machine.execCommands("ensuring has ssh public key", Arrays.asList("if [ ! -f " + privateKeyFile
                + " ] ; then ssh-keygen -t rsa -N \"\" -f " + privateKeyFile + " ; fi"));
        File kf = File.createTempFile("brooklyn", "machine.id_rsa.pub");
        machine.copyFrom(publicKeyFile, kf.getAbsolutePath());
        return Files.toString(kf, Charset.defaultCharset());
    } catch (Exception e) {
        throw Exceptions.propagate(e);
    }
}

From source file:org.t3as.ner.client.cmdline.Main.java

private static void processFiles(final Options opts, final NerClient client) throws IOException {
    // read each file and call the web service
    for (final File f : opts.files) {
        final String input = Files.toString(f, Charsets.UTF_8);
        System.out.printf("%s:\n", f);
        System.out.println(client.call(input));
    }//from   www .  j av a  2 s  .c o m
}

From source file:com.android.tools.idea.actions.license.LicenseTextCollector.java

@NotNull
private static String getLicenseText(@NotNull File f) {
    try {//from   ww w.ja  v  a2 s  .c  o m
        return Files.toString(f, Charsets.UTF_8).replaceAll("\\<.*?\\>", "").replace("\n", "<br>");
    } catch (IOException e) {
        return "";
    }
}

From source file:net.chris54721.infinitycubed.data.ForgeVersion.java

public static ForgeVersion fetch(String version) {
    try {// ww  w.  ja  va2  s  .  c om
        URL jsonUrl = Resources.getUrl(Resources.ResourceType.VERSION, version + ".json");
        File jsonFile = Resources.getFile(Resources.ResourceType.VERSION, version + ".json");
        Downloadable versionJson = new Downloadable(jsonUrl, jsonFile);
        versionJson.setForce(false);
        if (versionJson.download()) {
            String json = Files.toString(jsonFile, Charsets.UTF_8);
            return Reference.DEFAULT_GSON.fromJson(json, ForgeVersion.class);
        } else
            return null;
    } catch (Exception e) {
        LogHelper.error("Failed fetching JSON for Forge " + version, e);
        return null;
    }
}

From source file:at.jku.ce.adaptivetesting.vaadin.ui.LogView.java

@Override
public void enter(ViewChangeEvent event) {
    String fileContent;/*  www .  jav a  2s  . co  m*/
    try {
        fileContent = Files.toString(file, Charset.defaultCharset());
    } catch (IOException e) {
        LogHelper.logThrowable(e);
        fileContent = "Could not load file due to:" + e.getClass().getName() + " " + e.getMessage();
    }
    setValue(fileContent);
}

From source file:io.github.msdk.io.msp.MspImportAlgorithm.java

/**
 * <p>parseMspFromFile.</p>//from  w ww.j av a 2 s. com
 *
 * @param mspFile a {@link java.io.File} object.
 * @return a {@link io.github.msdk.io.msp.MspSpectrum} object.
 * @throws java.io.IOException if any.
 * @throws io.github.msdk.MSDKException if any.
 */
public static @Nonnull MspSpectrum parseMspFromFile(@Nonnull File mspFile) throws IOException, MSDKException {

    String str = Files.toString(mspFile, Charsets.ISO_8859_1);

    if (Strings.isNullOrEmpty(str))
        throw new IOException("Could not parse content of file " + mspFile);

    return parseMspFromString(str);
}