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:controllers.KeyPairController.java

@Transactional(readOnly = true)
public Result download(Long id) {
    KeyPair keyPair = loadEntity(id);

    if (keyPair == null) {
        return notFound(id);
    }/*  w w  w  .j  av  a 2 s. c  o  m*/

    //write temporary download file
    try {
        File file = File.createTempFile("id_rsa", keyPair.getUuid());
        Files.write(keyPair.getPrivateKey(), file, Charset.forName("UTF-8"));
        return ok(file);
    } catch (IOException e) {
        return internalServerError();
    }
}

From source file:org.obeonetwork.dsl.uml2.profile.design.profiletodsl.ManifestTools.java

/**
 * Add a given bundle to the require bundle of a manifest file.
 * /*w  ww  .  j av a  2s.  c o  m*/
 * @param bundle
 *            the bundle name
 * @param version
 *            the bundle version, can be <code>null</code>
 * @return the new manifest file content
 * @throws IOException
 */
public String addRequireBundle(String bundle, Version version) throws IOException {
    String originalContent = Files.toString(manifestFile, Charsets.UTF_8);
    String updatedFileContent = addRequireBundle(bundle, version, originalContent);
    if (!originalContent.equals(updatedFileContent)) {
        Files.write(updatedFileContent, manifestFile, Charsets.UTF_8);
        return updatedFileContent;
    }
    return originalContent;
}

From source file:com.geico.tfs.matcher.TfsMatcherWriter.java

public void endMatcherReportXmlFile() {
    appendLine(sb, "  </SonarReports>");
    try {//  www  .j a  v  a 2 s .c  o m
        Files.write(sb, new File(filename), Charsets.UTF_8);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.google.dart.java2dart.engine.MainEngine.java

public static void main(String[] args) throws Exception {
    if (args.length != 2 && args.length != 3) {
        System.out.println("Usage: java2dart <target-src-folder> <target-test-folder> [src-package]");
        System.exit(0);/* ww  w  .j  a va2s . c  om*/
    }
    String targetFolder = args[0];
    String targetTestFolder = args[1];
    if (args.length == 3) {
        System.out.println("Overrriding default src package to: " + src_package);
        src_package = args[2];
    }
    System.out.println("Generating files into " + targetFolder);
    new File(targetFolder).mkdirs();
    //
    engineFolder = new File("../../../tools/plugins/com.google.dart.engine/src");
    engineTestFolder = new File("../../../tools/plugins/com.google.dart.engine_test/src");
    engineFolder2 = new File("src");
    engineFolder = engineFolder.getCanonicalFile();
    // configure Context
    context.addClasspathFile(new File("../../../../third_party/guava/r13/guava-13.0.1.jar"));
    context.addClasspathFile(new File("../../../../third_party/junit/v4_8_2/junit.jar"));
    context.addSourceFolder(engineFolder);
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/utilities/ast"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/utilities/instrumentation"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/sdk"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/sdk"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/source"));
    context.addSourceFile(new File(engineFolder, "com/google/dart/engine/utilities/source/LineInfo.java"));
    context.addSourceFile(new File(engineFolder, "com/google/dart/engine/utilities/source/SourceRange.java"));
    context.addSourceFile(new File(engineFolder, "com/google/dart/engine/utilities/dart/ParameterKind.java"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/ast"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/ast/visitor"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/constant"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/element"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/error"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/html/ast"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/html/ast/visitor"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/html/parser"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/html/scanner"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/parser"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/resolver"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/scanner"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/type"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/builder"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/cache"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/constant"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/element"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/error"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/parser"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/resolver"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/scope"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/type"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/verifier"));
    context.addSourceFile(
            new File(engineFolder2, "com/google/dart/java2dart/util/ToFormattedSourceVisitor.java"));
    context.addSourceFile(new File(engineFolder, "com/google/dart/engine/AnalysisEngine.java"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/utilities/logging"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/context"));
    context.addSourceFiles(new File(engineFolder, "com/google/dart/engine/internal/context"));
    // Tests
    context.addSourceFile(
            new File(engineTestFolder, "com/google/dart/engine/utilities/io/FileUtilities2.java"));
    context.addSourceFile(new File(engineTestFolder, "com/google/dart/engine/EngineTestCase.java"));
    context.addSourceFile(
            new File(engineTestFolder, "com/google/dart/engine/error/GatheringErrorListener.java"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/scanner"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/parser"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/ast"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/element"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/internal/element"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/internal/type"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/resolver"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/internal/resolver"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/internal/scope"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/context"));
    context.addSourceFiles(new File(engineTestFolder, "com/google/dart/engine/internal/context"));
    // configure renames
    context.addRename("Lcom/google/dart/engine/ast/IndexExpression;.(Lcom/google/dart/engine/ast/Expression;"
            + "Lcom/google/dart/engine/scanner/Token;Lcom/google/dart/engine/ast/Expression;"
            + "Lcom/google/dart/engine/scanner/Token;)", "forTarget");
    context.addRename("Lcom/google/dart/engine/ast/IndexExpression;.(Lcom/google/dart/engine/scanner/Token;"
            + "Lcom/google/dart/engine/scanner/Token;Lcom/google/dart/engine/ast/Expression;"
            + "Lcom/google/dart/engine/scanner/Token;)", "forCascade");
    context.addRename(
            "Lcom/google/dart/engine/html/ast/XmlTagNode;.becomeParentOf<T:Lcom/google/dart/engine/html/ast/XmlNode;>(Ljava/util/List<TT;>;Ljava/util/List<TT;>;)",
            "becomeParentOfEmpty");
    // translate into single CompilationUnit
    dartUnit = context.translate();
    // run processors
    {
        List<SemanticProcessor> PROCESSORS = ImmutableList.of(new ConstructorSemanticProcessor(context),
                new ObjectSemanticProcessor(context), new CollectionSemanticProcessor(context),
                new IOSemanticProcessor(context), new PropertySemanticProcessor(context),
                new GuavaSemanticProcessor(context), new JUnitSemanticProcessor(context),
                new BeautifySemanticProcessor(context), new EngineSemanticProcessor(context));
        for (SemanticProcessor processor : PROCESSORS) {
            processor.process(dartUnit);
        }
    }
    // run this again, because we may introduce conflicts when convert methods to getters/setters
    context.ensureUniqueClassMemberNames(dartUnit);
    context.ensureNoVariableNameReferenceFromInitializer(dartUnit);
    context.ensureMethodParameterDoesNotHide(dartUnit);
    // handle reflection
    EngineSemanticProcessor.rewriteReflectionFieldsWithDirect(context, dartUnit);
    // dump as several libraries
    Files.copy(new File("resources/java_core.dart"), new File(targetFolder + "/java_core.dart"));
    Files.copy(new File("resources/java_io.dart"), new File(targetFolder + "/java_io.dart"));
    Files.copy(new File("resources/java_junit.dart"), new File(targetFolder + "/java_junit.dart"));
    Files.copy(new File("resources/java_engine.dart"), new File(targetFolder + "/java_engine.dart"));
    Files.copy(new File("resources/java_engine_io.dart"), new File(targetFolder + "/java_engine_io.dart"));
    Files.copy(new File("resources/all_test.dart"), new File(targetTestFolder + "/all_test.dart"));
    {
        CompilationUnit library = buildInstrumentationLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/instrumentation.dart"),
                Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildSourceLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/source.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildSourceIoLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/source_io.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildErrorLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/error.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildScannerLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/scanner.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildHtmlLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/html.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildUtilitiesDartLibrary();
        File astFile = new File(targetFolder + "/utilities_dart.dart");
        Files.write(getFormattedSource(library), astFile, Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildAstLibrary();
        File astFile = new File(targetFolder + "/ast.dart");
        Files.write(getFormattedSource(library), astFile, Charsets.UTF_8);
        Files.append(Files.toString(new File("resources/ast_include.dart"), Charsets.UTF_8), astFile,
                Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildParserLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/parser.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildSdkLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/sdk.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildSdkIoLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/sdk_io.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildConstantLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/constant.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildElementLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/element.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildResolverLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/resolver.dart"), Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildEngineLibrary();
        Files.write(getFormattedSource(library), new File(targetFolder + "/engine.dart"), Charsets.UTF_8);
    }
    // Tests
    {
        CompilationUnit library = buildTestSupportLibrary();
        File testSupportFile = new File(targetTestFolder + "/test_support.dart");
        Files.write(getFormattedSource(library), testSupportFile, Charsets.UTF_8);
        Files.append(Files.toString(new File("resources/test_support_include.dart"), Charsets.UTF_8),
                testSupportFile, Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildScannerTestLibrary();
        Files.write(getFormattedSource(library), new File(targetTestFolder + "/scanner_test.dart"),
                Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildParserTestLibrary();
        // replace reflection methods
        StringWriter methodWriter = new StringWriter();
        EngineSemanticProcessor.replaceReflectionMethods(context, new PrintWriter(methodWriter), dartUnit);
        // write to file
        File libraryFile = new File(targetTestFolder + "/parser_test.dart");
        Files.write(getFormattedSource(library), libraryFile, Charsets.UTF_8);
        Files.append(methodWriter.toString(), libraryFile, Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildAstTestLibrary();
        File astFile = new File(targetTestFolder + "/ast_test.dart");
        Files.write(getFormattedSource(library), astFile, Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildElementTestLibrary();
        Files.write(getFormattedSource(library), new File(targetTestFolder + "/element_test.dart"),
                Charsets.UTF_8);
    }
    {
        CompilationUnit library = buildResolverTestLibrary();
        Files.write(getFormattedSource(library), new File(targetTestFolder + "/resolver_test.dart"),
                Charsets.UTF_8);
    }
    System.out.println("Translation complete");
}

From source file:org.krobot.config.JSONConfig.java

@Override
public FileConfig save() {
    if (!file.exists()) {
        file.getParentFile().mkdirs();/*  w  ww .j a  v a2s.  co m*/
    }

    try {
        Files.write(gson.toJson(config), file, Charset.defaultCharset());
    } catch (IOException e) {
        throw new RuntimeException("Can't save the config", e);
    }

    return this;
}

From source file:com.theoryinpractise.coffeescript.ClosureMinifier.java

public void compile(List<File> filesToCompile, String destFileName) {
    File destFile = prepareDestFile(destFileName);

    Compiler compiler = new Compiler();
    Result results = compiler.compile(getExterns(), getInputs(filesToCompile), getCompilerOptions());

    logger.debug(results.debugLog);// w w  w  .ja  va 2s.  c o m
    for (JSError error : results.errors) {
        logger.error("Closure Minifier Error:  " + error.sourceName + "  Description:  " + error.description);
    }
    for (JSError warning : results.warnings) {
        logger.info(
                "Closure Minifier Warning:  " + warning.sourceName + "  Description:  " + warning.description);
    }

    if (results.success) {
        try {
            Files.write(compiler.toSource(), destFile, Charsets.UTF_8);
        } catch (IOException e) {
            throw new ClosureException("Failed to write minified file to " + destFile, e);
        }
    } else {
        throw new ClosureException("Closure Compiler Failed - See error messages on System.err");
    }
}

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

public void setupBuildGradle() throws IOException {
    String content = getBuildGradleContent();

    content += "dependencies {\n";
    for (GradleModule dep : projectDeps) {
        content += "  compile project('" + dep.getGradlePath() + "')\n";
    }//from  w ww. j  a  va2s .  co m
    content += "}\n";

    Files.write(content, new File(moduleDir, "build.gradle"), Charset.defaultCharset());
}

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

public static void generateProfile(Orchestrator orchestrator) {

    List<String> repositories = ImmutableList.of("jproperties", "sonarscanner");

    try {// w ww  .  j a va2  s .  c  o  m
        StringBuilder sb = new StringBuilder().append("<profile>").append("<name>rules</name>")
                .append("<language>jproperties</language>").append("<rules>");

        for (String repository : repositories) {
            Set<String> ruleKeys = getRuleKeysFromRepository(repository, orchestrator);

            for (String key : ruleKeys) {
                sb.append("<rule>").append("<repositoryKey>").append(repository).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:org.eclipse.buildship.core.workspace.internal.ClasspathContainerPersistence.java

void save(List<IClasspathEntry> entries) {
    StringBuilder content = new StringBuilder();
    content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    content.append("<classpath>\n");
    for (IClasspathEntry entry : entries) {
        content.append(this.javaProject.encodeClasspathEntry(entry));
    }/*from w w w  .  ja v a2 s  . co  m*/
    content.append("</classpath>\n");
    File stateLocation = getStateLocation();
    try {
        Files.createParentDirs(stateLocation);
        Files.write(content, stateLocation, Charsets.UTF_8);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

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

private void massageGrammar(String absoluteParserFileName, String encoding) {
    try {// w w  w  .  j  a  va 2  s .c om
        String javaFile = absoluteParserFileName.replaceAll("\\.g$", getParserFileNameSuffix());
        String content = Files.toString(new File(javaFile), Charset.forName(encoding));
        String normalizedContent = content.replace("\r\n", "\n");

        String newContent = fixIdentifierAsKeywordWithEOLAwareness(normalizedContent);

        if (normalizedContent.equals(newContent)) {
            log.warn("Replacement not found in " + javaFile);
            // throw new IllegalStateException("Replacement not found in " + javaFile);
        }
        if (!content.equals(newContent)) {
            Files.write(newContent, new File(javaFile), Charset.forName(encoding));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}