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:org.yes.cart.installer.ApacheTomcat7Configurer.java

private void configureHTTP() throws IOException {
    File serverXMLFile = new File(tomcatHome, "conf" + File.separator + "server.xml");
    Files.write(/*w  ww  .  j ava2s .c  o m*/
            Files.toString(serverXMLFile, Charset.forName("UTF-8")).replace("8080", String.valueOf(httpPort)),
            serverXMLFile, Charset.forName("UTF-8"));
}

From source file:org.lesscss.LessSource.java

/**
 * Constructs a new <code>LessSource</code>.
 * <p>/*from w  w w . ja v  a2  s . c om*/
 * This will read the metadata and content of the LESS source, and will automatically resolve the imports. 
 * </p>
 * 
 * @param file The <code>File</code> reference to the LESS source to read.
 * @throws FileNotFoundException If the LESS source (or one of its imports) could not be found.
 * @throws IOException If the LESS source cannot be read.
 */
public LessSource(File file) throws FileNotFoundException, IOException {
    Preconditions.checkNotNull(file, "File must not be null.");
    if (!file.exists()) {
        throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found.");
    }
    this.file = file;
    this.content = this.normalizedContent = Files.toString(file, Charsets.UTF_8);
    resolveImports();
}

From source file:org.opendaylight.controller.config.persist.storage.file.xml.model.Config.java

private static boolean isBlank(File from) {
    try {/*from  w  w w . j a v  a2 s. c o m*/
        return StringUtils.isBlank(Files.toString(from, StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected error reading file" + from, e);
    }
}

From source file:org.glowroot.plugin.cassandra.CassandraWrapper.java

private static void downloadAndExtract(File baseDir) throws MalformedURLException, IOException {
    URL url = new URL("http://archive.apache.org/dist/cassandra/" + CASSANDRA_VERSION + "/apache-cassandra-"
            + CASSANDRA_VERSION + "-bin.tar.gz");
    InputStream in = url.openStream();
    File archiveFile = File.createTempFile("cassandra-" + CASSANDRA_VERSION + "-", ".tar.gz");
    Files.asByteSink(archiveFile).writeFrom(in);
    in.close();//from  w  w  w.  j  a v  a2 s  . c o m
    Archiver archiver = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP);
    archiver.extract(archiveFile, baseDir);
    archiveFile.delete();

    File cassandraDir = new File(baseDir, "apache-cassandra-" + CASSANDRA_VERSION);
    File confDir = new File(cassandraDir, "conf");
    File yamlFile = new File(confDir, "cassandra.yaml");
    String yaml = Files.toString(yamlFile, Charsets.UTF_8);
    yaml = yaml.replace("/var/lib/cassandra", cassandraDir.getAbsolutePath().replace('\\', '/'));
    Files.asCharSink(yamlFile, Charsets.UTF_8).write(yaml);

    File log4jFile = new File(confDir, "log4j-server.properties");
    String log4j = Files.toString(log4jFile, Charsets.UTF_8);
    log4j = log4j.replace("/var/log/cassandra", cassandraDir.getAbsolutePath().replace('\\', '/'));
    Files.asCharSink(log4jFile, Charsets.UTF_8).write(log4j);
}

From source file:eu.numberfour.n4js.antlr.delimiters.DebugAntlrGeneratorFragment.java

@SuppressWarnings("resource")
private void prettyPrint(String absoluteGrammarFileName, String encoding) {
    try {/*from w w w .  jav a  2s .  c o m*/
        String content = Files.toString(new File(absoluteGrammarFileName), Charset.forName(encoding));
        final ILineSeparatorInformation unixLineSeparatorInformation = new ILineSeparatorInformation() {
            @Override
            public String getLineSeparator() {
                return NewlineNormalizer.UNIX_LINE_DELIMITER;
            }
        };
        Injector injector = new SimpleAntlrStandaloneSetup() {
            @Override
            public Injector createInjector() {
                return Guice.createInjector(new SimpleAntlrRuntimeModule() {
                    @Override
                    public void configure(Binder binder) {
                        super.configure(binder);
                        binder.bind(ILineSeparatorInformation.class).toInstance(unixLineSeparatorInformation);
                    }

                    @Override
                    public Class<? extends IFormatter> bindIFormatter() {
                        return PlatformIndependantFormatter.class;
                    }
                });
            }
        }.createInjectorAndDoEMFRegistration();
        XtextResource resource = injector.getInstance(XtextResource.class);
        resource.setURI(URI.createFileURI(absoluteGrammarFileName));

        // no need to close StringInputStream since it doesn't allocate resources
        resource.load(new StringInputStream(content, encoding),
                Collections.singletonMap(XtextResource.OPTION_ENCODING, encoding));
        if (!resource.getErrors().isEmpty()) {
            throw new RuntimeException(resource.getErrors().toString());
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(content.length());
        resource.save(outputStream, SaveOptions.newBuilder().format().getOptions().toOptionsMap());
        String postprocessContent = new NewlineNormalizer()
                .toUnixLineDelimiter(new String(outputStream.toByteArray(), encoding));
        Files.write(postprocessContent, new File(absoluteGrammarFileName), Charset.forName(encoding));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.numberfour.n4js.antlr.compressor.ParserCompressorFragment2.java

@Override
public void generate() {
    for (String fileName : grammarFiles) {
        File file = new File(fileName);
        String javaSource = null;
        try {/*w ww .  j a  v  a 2s  .c  o  m*/
            javaSource = Files.toString(file, Charsets.UTF_8);
        } catch (Exception ex) {
            LOGGER.error("Error reading file " + fileName + ": " + ex.getMessage());
        }
        if (javaSource != null) {
            String compressed = process(javaSource, file);
            LOGGER.info("File " + readableFileName(file) + " compressed: " + javaSource.length() + " --> "
                    + compressed.length() + " (" + 100 * compressed.length() / javaSource.length() + "%)");

            if (backup) {
                try {
                    Files.copy(file, new File(file.getParentFile(), file.getName() + ".bak"));
                } catch (IOException e) {
                    LOGGER.error("Error creating backup of " + readableFileName(file) + ": " + e.getMessage());
                    return;
                }
            }

            try {
                Files.write(compressed, file, Charsets.UTF_8);
            } catch (IOException e) {
                LOGGER.error("Error writing compressed file " + readableFileName(file) + ": " + e.getMessage());
            }
        }
    }
}

From source file:org.jetbrains.jet.compiler.NamespaceComparator.java

public static void compareNamespaces(@NotNull NamespaceDescriptor nsa, @NotNull NamespaceDescriptor nsb,
        boolean includeObject, @NotNull File txtFile) {
    String serialized = new NamespaceComparator(includeObject).doCompareNamespaces(nsa, nsb);
    try {//from  ww w. j  a va 2s. c  o m
        for (;;) {
            String expected = Files.toString(txtFile, Charset.forName("utf-8")).replace("\r\n", "\n");

            if (expected.contains("kick me")) {
                // for developer
                System.err.println("generating " + txtFile);
                Files.write(serialized, txtFile, Charset.forName("utf-8"));
                continue;
            }

            // compare with hardcopy: make sure nothing is lost in output
            Assert.assertEquals(expected, serialized);
            break;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.testing.pogen.ListCommand.java

@Override
public void execute() throws IOException {
    for (String templatePath : templatePaths) {
        TemplateParser templateParser = TemplateParsers.getPreferredParser(templatePath, attributeName);
        File templateFile = createFileFromFilePath(templatePath);
        checkExistenceAndPermission(templateFile, true, false);
        String template = Files.toString(templateFile, Charset.defaultCharset());
        try {/*from  w  w w .jav  a  2s.co m*/
            TemplateInfo templateInfo = templateParser.parse(template);
            for (HtmlTagInfo tagInfo : templateInfo.getHtmlTagInfos()) {
                for (@SuppressWarnings("unused")
                VariableInfo varInfo : tagInfo.getVariableInfos()) {
                    String id = tagInfo.hasAttributeValue() ? tagInfo.getAttributeValue() : "";
                    System.out.print(templatePath + ", " + id);
                    for (VariableInfo variableInfo : tagInfo.getVariableInfos()) {
                        if (!variableInfo.isManipulableTag()) {
                            System.out.print(", " + variableInfo.getName());
                        }
                    }
                    System.out.println();
                }
            }
        } catch (TemplateParseException e) {
            throw new FileProcessException("Errors occur in parsing the specified file", templateFile, e);
        }
    }
}

From source file:com.blackboard.chef.api.ChefRoleSelectModel.java

private ChefApi getChefServer(String server, String user) throws IOException {
    System.out.println("DEBUG: Entering getChefServer");
    String client = user;//from  w w  w .j  av a  2 s.  co  m
    String homeDir = System.getProperty("user.home");
    String pemFile = homeDir + "/.chef/" + user + ".pem";
    String credential = Files.toString(new File(pemFile), UTF_8);
    // Example below
    // context = ContextBuilder.newBuilder("chef")
    //     .endpoint("https://chef.mhint")
    //     .credentials(client, credential)
    //     .buildView(ChefContext.class);
    ChefContext context = ContextBuilder.newBuilder("chef").endpoint(server).credentials(client, credential)
            .buildView(ChefContext.class);
    ChefApi api = context.getApi(ChefApi.class);
    return (api);
}

From source file:com.smartbear.soapui.utils.jetty.JettyTestCaseBase.java

protected void replaceInFile(String fileName, String from, String to) throws IOException {
    File wsdlFile = new File(getResourceBase(), fileName);
    String wsdl = Files.toString(wsdlFile, Charset.forName("UTF-8"));

    wsdl = wsdl.replace(from, to);//from  w ww.j  a  va 2  s  . c om

    Files.write(wsdl, wsdlFile, Charset.forName("UTF-8"));
}