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.radonix.moted.service.FileService.java

public void loadSource(final File file, final SourceEditor editor) {
    executeTask(new Task<String>() {
        @Override// w w  w  .  j  ava2  s.  c o  m
        public String atWorking() throws Exception {
            String source = null;
            if (file.exists() && file.isFile()) {
                String ext = Files.getFileExtension(file.getName());
                if (Const.TEXT_FILE_EXTS.contains(ext)) {
                    source = Files.toString(file, Charset.defaultCharset());
                } else {
                    // File type not supported.
                    // TODO
                }
            } else {
                // Cannot find the file.
                // TODO
            }
            return Strings.nullToEmpty(source);
        }

        @Override
        public void onSuccess(String source) {
            editor.setSource(source);
        }

        @Override
        public void onFailure(Exception e) {
            Log.e(TAG, "Cannot read from " + file.getName(), e);
        }
    });
}

From source file:com.android.tools.idea.jps.output.parser.aapt.ReadOnlyDocument.java

/**
 * Creates a new {@link ReadOnlyDocument} for the given file.
 *
 * @param file the file whose text will be stored in the document. UTF-8 charset is used to decode the contents of the file.
 * @throws java.io.IOException if an error occurs while reading the file.
 *///from  w ww  . j a va2s.  co m
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
ReadOnlyDocument(@NotNull File file) throws IOException {
    myContents = Files.toString(file, Charsets.UTF_8);
    myOffsets = Lists.newArrayListWithExpectedSize(myContents.length() / 30);
    myOffsets.add(0);
    for (int i = 0; i < myContents.length(); i++) {
        char c = myContents.charAt(i);
        if (c == '\n') {
            myOffsets.add(i + 1);
        }
    }
}

From source file:org.apache.brooklyn.location.jclouds.templates.customize.LoginUserPrivateKeyFileOption.java

@Override
public void apply(TemplateOptions t, ConfigBag props, Object v) {
    if (v != null) {
        String privateKeyFileName = v.toString();
        String privateKey;//from   w w  w.  j a va  2 s .  c  o m
        try {
            privateKey = Files.toString(new File(Os.tidyPath(privateKeyFileName)), Charsets.UTF_8);
        } catch (IOException e) {
            LOG.error(privateKeyFileName + "not found", e);
            throw Exceptions.propagate(e);
        }
        t.overrideLoginPrivateKey(privateKey);
    }
}

From source file:com.cueup.hegemon.PathScriptLocator.java

@Override
public String getFile(String name) throws LoadError {
    File script = new File(this.root, name);
    try {/*  w w  w.  j  a va 2 s.  c  om*/
        if (script.getCanonicalPath().startsWith(this.root.getCanonicalPath()) && script.exists()) {
            return Files.toString(script, Charsets.UTF_8);
        }
    } catch (IOException e) {
        throw new LoadError(e);
    }
    throw new LoadError("Could not find " + name + " under " + this.root);
}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.vstest.results.VSTestStdOutParser.java

public void setFile(File resultsFile) {
    try {//from   w w w  .  j  a v  a 2 s. co m
        results = Files.toString(resultsFile, Charset.defaultCharset());
    } catch (IOException e) {
        throw new SonarException("Failed to read", e);
    }
}

From source file:org.freeeed.services.Util.java

/**
 * @param fileName/*from ww w.j av  a 2  s  .c om*/
 * @return content of the file
 */
public static String readTextFile(String fileName) throws IOException {
    return Files.toString(new File(fileName), Charset.defaultCharset());
}

From source file:net.ripe.rpki.validator.util.TrustAnchorLocator.java

public static TrustAnchorLocator fromFile(File file) throws TrustAnchorExtractorException {
    try {//  ww  w  .  jav a 2 s.  c o  m
        String contents = Files.toString(file, Charsets.UTF_8);
        if (contents.trim().startsWith("rsync://")) {
            return readStandardTrustAnchorLocator(file, contents);
        } else {
            return readExtendedTrustAnchorLocator(file, contents);
        }
    } catch (IllegalArgumentException e) {
        throw new TrustAnchorExtractorException(
                "failed to load trust anchor locator " + file + ": " + e.getMessage(), e);
    } catch (IOException e) {
        throw new TrustAnchorExtractorException(
                "failed to open trust anchor locator " + file + ": " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        throw new TrustAnchorExtractorException(
                "failed to load trust anchor locator " + file + ": " + e.getMessage(), e);
    }
}

From source file:com.google.api.codegen.util.MultiYamlReader.java

@Nullable
public static ConfigSource read(DiagCollector collector, List<File> files,
        Map<String, Message> supportedConfigTypes) {
    List<String> inputNames = new ArrayList<>();
    List<String> inputs = new ArrayList<>();
    for (File file : files) {
        inputNames.add(file.getName());/*from   w w  w.ja va 2  s  .c  o  m*/
        try {
            String fileContent = Files.toString(file, Charset.forName("UTF8"));
            inputs.add(fileContent);
        } catch (IOException e) {
            collector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Cannot read configuration file '%s': %s",
                    file.getName(), e.getMessage()));
        }
    }
    if (collector.getErrorCount() > 0) {
        return null;
    } else {
        return read(collector, inputNames, inputs, supportedConfigTypes);
    }
}

From source file:com.android.build.gradle.internal.tasks.MergeFileTask.java

@TaskAction
public void mergeFiles() throws IOException {

    Set<File> files = getInputFiles();
    File output = getOutputFile();

    if (files.size() == 1) {
        Files.copy(files.iterator().next(), output);
        return;//from   w  w  w. ja v a2  s.  c o m
    }

    // first delete the current file
    output.delete();

    // no input? done.
    if (files.isEmpty()) {
        return;
    }

    // otherwise put the all the files together
    for (File file : files) {
        String content = Files.toString(file, Charsets.UTF_8);
        Files.append(content, output, Charsets.UTF_8);
        Files.append("\n", output, Charsets.UTF_8);
    }
}

From source file:com.google.dart.tools.internal.corext.refactoring.base.DartStatusContext_NEW.java

public DartStatusContext_NEW(Location location) {
    file = location.getFile();/*from   w w  w . jav a2s  . co  m*/
    Assert.isNotNull(file);
    try {
        this.content = Files.toString(new File(file), Charsets.UTF_8);
    } catch (Throwable e) {
        this.content = e.getMessage();
    }
    this.region = new Region(location.getOffset(), location.getLength());
}