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

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

Introduction

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

Prototype

public static void write(CharSequence from, File to, Charset charset) throws IOException 

Source Link

Usage

From source file:org.apache.sentry.tests.e2e.hive.PolicyFileEditor.java

public void addPolicy(String line, String cat) throws IOException {
    List<String> result = new ArrayList<String>();
    boolean exist = false;
    for (String s : Files.readLines(policy, Charsets.UTF_8)) {
        result.add(s);/* w w  w . j  a  va  2s  .c om*/
        if (s.equals("[" + cat + "]")) {
            result.add(line);
            exist = true;
        }
    }
    if (!exist) {
        result.add("[" + cat + "]");
        result.add(line);
    }
    Files.write(Joiner.on(NL).join(result), policy, Charsets.UTF_8);
}

From source file:org.bonitasoft.studio.common.FileUtil.java

public static void replaceStringInFile(File file, String match, String replacingString) {
    try {//from   w ww.  ja  v  a  2 s .  c o  m
        final Charset utf8 = Charset.forName("UTF-8");
        Files.write(Files.toString(file, utf8).replace(match, replacingString), file, utf8);
    } catch (final IOException e) {
        BonitaStudioLog.error(e);
    }

}

From source file:org.sonar.css.its.ProfileGenerator.java

public static void generateProfile(Orchestrator orchestrator, String language) {
    try {//from  w ww  .j a va2  s .  c o  m
        StringBuilder sb = new StringBuilder().append("<profile>").append("<name>rules</name>")
                .append("<language>").append(language).append("</language>").append("<rules>");

        Set<String> ruleKeys = getRuleKeysFromRepository(orchestrator, language);

        for (String key : ruleKeys) {
            sb.append("<rule>").append("<repositoryKey>").append(language).append("</repositoryKey>")
                    .append("<key>").append(key).append("</key>").append("<priority>INFO</priority>");

            Collection<Parameter> parameters = ProfileGenerator.parameters.get(key);
            if (!parameters.isEmpty()) {
                sb.append("<parameters>");
                for (Parameter parameter : parameters) {
                    sb.append("<parameter>").append("<key>").append(parameter.parameterKey).append("</key>")
                            .append("<value>").append(parameter.parameterValue).append("</value>")
                            .append("</parameter>");
                }
                sb.append("</parameters>");
            }

            sb.append("</rule>");
        }
        sb.append("</rules>").append("</profile>");

        File file = File.createTempFile("profile", ".xml");
        Files.write(sb, file, Charsets.UTF_8);
        orchestrator.getServer().restoreProfile(FileLocation.of(file));
        file.delete();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.github.rinde.rinsim.scenario.measure.MetricsIO.java

/**
 * Writes the specified list of numbers to a file. The list is interpreted as
 * the y-values of a graph, the indices of the list are used as the x-values
 * of the graph. A <code>0</code> is always appended. For example, if the
 * provided list is <code>[10, 20, 30, 40]</code>, the file will contain:
 *
 * <pre>/*  w w w  .  ja  va2s . co m*/
 * {@code 0 10}
 * {@code 1 20}
 * {@code 2 30}
 * {@code 3 40}
 * {@code 4 0}
 * </pre>
 *
 * @param list The list of numbers to write to a file.
 * @param file The file to write to.
 */
public static void writeLoads(List<? extends Number> list, File file) {
    final StringBuilder sb = new StringBuilder();
    int i = 0;
    for (; i < list.size(); i++) {
        sb.append(i).append(SPACE).append(list.get(i)).append(System.lineSeparator());
    }
    sb.append(i).append(SPACE).append(0).append(System.lineSeparator());
    try {
        Files.createParentDirs(file);
        Files.write(sb.toString(), file, Charsets.UTF_8);
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:eu.numberfour.n4js.antlr.AntlrToolFacadeWithInjectedCode.java

private void injectCode(String grammarFullPath) {
    try {//from   w  w  w  .  j  a v  a2s.  c o  m
        String grammarContent = Files.toString(new File(grammarFullPath), Charsets.UTF_8);
        if (grammarFullPath.endsWith("Lexer.g")) {
            grammarContent = processLexerGrammar(grammarContent);
        } else if (grammarFullPath.endsWith("Parser.g")) {
            grammarContent = processParserGrammar(grammarContent);
        } else {
            throw new IllegalArgumentException(grammarFullPath);
        }
        Files.write(grammarContent, new File(grammarFullPath), Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException();
    }
}

From source file:org.artifactory.support.core.collectors.AbstractGenericContentCollector.java

/**
 * Collects security info/*w w  w .  j  a va2  s.com*/
 *
 * @param configuration {@link org.artifactory.support.config.CollectConfiguration}
 * @param tmpDir output dir
 *
 * @return result
 */
protected final boolean doCollect(T configuration, File tmpDir) {
    if (configuration.isEnabled()) {
        try {
            StringBuilderWrapper content = doProduceContent(configuration);
            Files.write(content, getOutputFile(tmpDir), Charsets.UTF_8);
            getLog().info("Collection of " + getContentName() + " was successfully accomplished");
            return true;
        } catch (IOException | InstantiationException | IllegalAccessException
                | ContentCollectionExceptionException e) {
            getLog().error("Collecting " + getContentName() + " has failed, - " + e.getMessage());
            getLog().debug("Cause: {}", e);
        }
    } else {
        getLog().debug("Content collection of " + getContentName() + " is disabled");
    }
    return false;
}

From source file:org.apache.whirr.service.FileClusterStateStore.java

@Override
public void save(Cluster cluster) throws IOException {
    File instancesFile = new File(spec.getClusterDirectory(), "instances");

    try {//from www. ja  v  a 2 s. co m
        Files.write(serialize(cluster).toString(), instancesFile, Charsets.UTF_8);
        LOG.info("Wrote instances file {}", instancesFile);

    } catch (IOException e) {
        LOG.error("Problem writing instances file {}", instancesFile, e);
    }
}

From source file:com.android.build.gradle.integration.common.fixture.app.AndroidGradleModule.java

@Override
public void createFiles() throws IOException {
    File root = getModuleDir();/*from w  ww  .j ava 2  s .  c  o  m*/
    File src = new File(root, "src");
    File main = new File(src, "main");
    main.mkdirs();

    File manifest = new File(main, "AndroidManifest.xml");

    Files.write(
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                    + "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
                    + "    package=\"com" + getGradlePath().replace(':', '.') + "\" />\n",
            manifest, Charsets.UTF_8);
}

From source file:com.mapr.storm.SpoutState.java

public static void recordCurrentState(Map<Long, PendingMessage> ackBuffer, DirectoryScanner scanner,
        StreamParser parser, File statusFile) {
    try {//  w  w w . j a  v a  2s . c  o  m
        // find smallest offset for each file
        Map<File, Long> offsets = Maps.newHashMap();
        for (PendingMessage m : ackBuffer.values()) {
            Long x = offsets.get(m.getFile());
            if (x == null) {
                x = m.getOffset();
                offsets.put(m.getFile(), x);
            }
            if (m.getOffset() < x) {
                offsets.put(m.getFile(), x);
            }
        }

        Long x = offsets.get(scanner.getLiveFile());
        if (x == null) {
            offsets.put(scanner.getLiveFile(), parser.currentOffset());
        }

        final String jsonState = new Gson().toJson(new SpoutState(scanner, offsets));
        final File newState = new File(statusFile.getParentFile(),
                String.format("%s-%06x", statusFile.getName(), new Random().nextInt()));
        Files.write(jsonState, newState, Charsets.UTF_8);
        Files.move(newState, statusFile);
    } catch (IOException e) {
        log.error(String.format("Unable to write status to %s", statusFile), e);
    }
}

From source file:com.github.benmanes.caffeine.cache.simulator.report.TextReport.java

/** Writes the report to the output destination. */
public void print() throws IOException {
    String output = settings.report().output();
    String results = assemble();//from w w  w.ja  va  2  s.  co m
    if (output.equalsIgnoreCase("console")) {
        System.out.println(results);
    } else {
        File file = Paths.get(output).toFile();
        Files.write(results, file, Charsets.UTF_8);
    }
}