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.android.build.gradle.integration.common.utils.FileHelper.java

public static void checkContent(File file, Iterable<String> expectedContents) throws IOException {
    assertTrue("File '" + file.getAbsolutePath() + "' does not exist.", file.isFile());

    String contents = Files.toString(file, Charsets.UTF_8);
    for (String expectedContent : expectedContents) {
        assertTrue("File '" + file.getAbsolutePath() + "' does not contain: " + expectedContent,
                contents.contains(expectedContent));
    }//from   w  w w.  j  a v  a 2 s.co  m
}

From source file:org.apache.aurora.scheduler.TierModule.java

static String readTierFile() {
    try {//from   w w w .  j  av a  2  s.com
        return TIER_CONFIG_FILE.hasAppliedValue()
                ? Files.toString(TIER_CONFIG_FILE.get(), StandardCharsets.UTF_8)
                : Resources.toString(TierModule.class.getClassLoader().getResource(TIER_CONFIG_PATH),
                        StandardCharsets.UTF_8);
    } catch (IOException e) {
        LOG.error("Error loading tier configuration file.");
        throw new RuntimeException(e);
    }
}

From source file:org.ops4j.pax.carrot.ui.EditorController.java

public void loadFile() {
    file = treeBean.getSelectedFile();/* w w w.  j a  v  a2  s .c o  m*/
    if (file == null || !file.exists() || file.isDirectory()) {
        return;
    }
    content = "";
    try {
        content = Files.toString(file, UTF8);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.xmlsh.aws.gradle.elasticbeanstalk.EbConfigurationTemplateExtension.java

public String getOptionSettings() throws IOException {
    if (optionSettings instanceof Closure) {
        Closure<?> closure = (Closure<?>) optionSettings;
        return closure.call().toString();
    }/*from ww  w . ja va  2 s. com*/
    if (optionSettings instanceof File) {
        File file = (File) optionSettings;
        return Files.toString(file, Charsets.UTF_8);
    }
    return optionSettings.toString();
}

From source file:org.jclouds.oauth.v2.OAuthTestUtils.java

public static String setCredentialFromPemFile(Properties overrides, String key) {
    String val = null;
    String credentialFromFile = null;
    String testKey = "test." + key;

    if (System.getProperties().containsKey(testKey)) {
        val = System.getProperty(testKey);
    }//w w w .ja v  a  2 s .  c o  m
    checkNotNull(val, String.format("the property %s must be set (pem private key path)", testKey));

    try {
        credentialFromFile = Files.toString(new File(val), Charsets.UTF_8);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    overrides.setProperty(key, credentialFromFile);
    return credentialFromFile;
}

From source file:datamine.mojo.storage.TableMetadataGenerationMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    validateArguments();/*ww  w .  jav  a  2 s. c om*/

    try {
        //0. generate the java source codes
        Schema schema = new JsonSchemaConvertor().apply(Files.toString(schemaPath, Charsets.UTF_8));

        //1. generate model codes
        MetadataFileGenerator generator = new MetadataFileGenerator(outputDirectory.getAbsolutePath(),
                packageName);
        generator.apply(schema);

    } catch (IOException e) {
        throw new MojoExecutionException("Error generating Java classes for table schema!", e);
    }
}

From source file:pl.iz.cubicrl.controller.core.ScriptLoader.java

public void initializeScripts() throws IOException, ScriptException {
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine engine = m.getEngineByName("nashorn");
    SimpleBindings bindings = new SimpleBindings();
    bindings.put("eventBus", eventBus);
    bindings.put("itemActionExecutor", actionExecutor);
    engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
    StringBuilder sb = new StringBuilder();
    sb.append(Files.toString(new File("../Scripts/utils.js"), Charset.forName("UTF-8")));
    java.nio.file.Files.walk(Paths.get("../Scripts"))
            //.filter(file -> file.endsWith("js") && !file.endsWith("utils.js"))
            .forEach(f -> {//from w w w .  j av a 2 s.  c  o  m
                try {
                    System.out.println(
                            "########################################################################################");
                    System.out.println(f);
                    sb.append(Files.toString(new File(f.getFileName().toUri()), Charset.forName("UTF-8")));
                } catch (IOException ex) {
                    System.out.println(ex);
                }
            });
    System.out.println(sb.toString());
    engine.eval(sb.toString());
}

From source file:com.google.api.codegen.configgen.ConfigYamlReader.java

public FieldConfigNode generateConfigNode(File file, ConfigHelper helper) {
    int initialErrorCount = helper.getErrorCount();
    String input;/*from  w w w  .j  a  va 2 s .c o m*/
    try {
        input = Files.toString(file, Charsets.UTF_8);
    } catch (IOException e) {
        helper.error("Cannot read configuration file: %s", e.getMessage());
        return null;
    }

    if (input.trim().isEmpty()) {
        helper.error("Empty YAML document");
        return null;
    }

    Node tree;
    try {
        tree = new Yaml().compose(new StringReader(input));
    } catch (ComposerException e) {
        helper.error(e.getProblemMark(), "Parsing error: %s", e.getMessage());
        return null;
    } catch (Exception e) {
        helper.error("Parsing error: %s", e.getMessage());
        return null;
    }

    if (tree == null) {
        helper.error("Parsing error");
        return null;
    }

    if (!(tree instanceof MappingNode)) {
        helper.error(tree, "Expected a map as a root object.");
        return null;
    }

    List<String> lines = Splitter.on(System.lineSeparator()).splitToList(input);
    ConfigNode configNode = new ConfigYamlNodeReader(lines, helper).readMessageNode((MappingNode) tree,
            ConfigProto.getDescriptor());
    return helper.getErrorCount() == initialErrorCount ? new FieldConfigNode(0, "").setChild(configNode) : null;
}

From source file:hihex.cs.PidDatabase.java

/**
 * Finds the process name for a process ID.
 *
 * @param pidString The process ID, as a string (e.g. "1234").
 * @return The process name. If the process is already dead, a dummy representation will be returned.
 *//*from   w  ww . j ava  2s .  com*/
private static String getProcessName(final String pidString) {
    try {
        final File file = new File("/proc/" + pidString + "/cmdline");
        final String processName = Files.toString(file, Charsets.UTF_8).trim();
        if (!processName.isEmpty()) {
            final String[] arguments = processName.split("\0");
            final StringBuilder builder = new StringBuilder();
            boolean isFirst = true;
            for (final String argument : arguments) {
                if (isFirst) {
                    isFirst = false;
                } else {
                    builder.append(' ');
                }
                if (TextUtils.isEmpty(argument)) {
                    builder.append("''");
                } else if (!UNSAFE_SHELL_CHARACTERS.matcher(argument).find()) {
                    builder.append(argument);
                } else {
                    builder.append('\'');
                    builder.append(argument.replace("'", "'\"'\"'"));
                    builder.append('\'');
                }
            }
            return builder.toString();
        }
    } catch (IOException e) {
        // Ignore.
    }

    try {
        final File file = new File("/proc/" + pidString + "/comm");
        final String processName = Files.toString(file, Charsets.UTF_8).trim();
        if (!processName.isEmpty()) {
            return processName;
        }
    } catch (IOException e) {
        // Ignore.
    }

    return "PID:" + pidString;
}

From source file:org.moe.designer.rendering.LayoutFilePullParser.java

/**
 * Crates a new {@link LayoutFilePullParser
 *///from   w ww.j a v a2  s . c om
public static LayoutFilePullParser create(@NotNull IProjectCallback projectCallback, @NotNull File xml)
        throws XmlPullParserException, IOException {
    LayoutFilePullParser parser = new LayoutFilePullParser(projectCallback);
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    String xmlText = Files.toString(xml, Charsets.UTF_8);
    parser.setInput(new StringReader(xmlText));
    return parser;
}