Example usage for java.nio.file Files readAllLines

List of usage examples for java.nio.file Files readAllLines

Introduction

In this page you can find the example usage for java.nio.file Files readAllLines.

Prototype

public static List<String> readAllLines(Path path, Charset cs) throws IOException 

Source Link

Document

Read all lines from a file.

Usage

From source file:org.eclipse.ice.item.utilities.moose.MOOSEFileHandler.java

/**
 * This method is responsible for loading the action syntax file associated
 * with a MOOSE app. It reads through the list of paths and returns the
 * names of any that are "hard" paths (ie. have no asterisks). It also only
 * returns unique paths, as there is no significance to duplicates (to us)
 * in the action syntax file (it's a MOOSE "bug").
 * /*w  w w . j  a  v a2  s  .c o m*/
 * @param filePath
 *            The file path pointing to the action syntax file
 * @return A String ArrayList of unique action syntax "hard" paths (ie. does
 *         not end in an asterisk)
 * @throws IOException
 */
public ArrayList<String> loadActionSyntax(String filePath) throws IOException {

    // Local declarations
    ArrayList<String> actionSyntax = null;
    String currLine, previousLine = "";

    // Check if the filepath is valid
    if (filePath == null || filePath.isEmpty()) {
        if (debugFlag) {
            logger.info("MOOSEFileHandler Error: Could not open " + "action syntax file: " + filePath);
        }
        return actionSyntax;
    }

    // Open the action syntax file
    if (debugFlag) {
        logger.info("MOOSEFileHandler Message: Loading action " + "syntax file: " + filePath);
    }

    actionSyntax = (ArrayList<String>) Files.readAllLines(Paths.get(filePath), Charset.defaultCharset());

    // Iterate through the list and eliminate non-hard-paths and
    // duplicate entries
    int i = 0;
    while (i < actionSyntax.size()) {

        // Get the current line
        currLine = actionSyntax.get(i);

        // Check for an asterisk at the end of the line
        if (currLine.endsWith("*\r") || currLine.endsWith("*")) {
            actionSyntax.remove(currLine);
        }

        // Remove from the ArrayList if it's the same as the last line
        else if (previousLine.equals(currLine)) {
            actionSyntax.remove(currLine);
        }

        // Otherwise it's a unique hard-path, and move to the next line
        else {
            previousLine = currLine;
            i++;
        }
    }

    return actionSyntax;
}

From source file:com.streamsets.datacollector.cluster.TestShellClusterProvider.java

@Test
public void testCopyDpmToken() throws Exception {
    Properties sdcProperties = new Properties();
    sdcProperties.put(Configuration.CONFIG_INCLUDES, "dpm-test.properties");
    File etcDir = tempFolder.newFolder();
    Properties dpmProperties = new Properties();
    dpmProperties.setProperty("foo", "fooVal");
    dpmProperties.setProperty(RemoteSSOService.DPM_ENABLED, "true");
    File appTokenFile = tempFolder.newFile();
    try (PrintWriter out = new PrintWriter(appTokenFile)) {
        out.println("app-token-dummy-text");
    }//from  www. jav  a 2  s.c o m
    // dpm enabled and app token is absolute
    dpmProperties.setProperty(RemoteSSOService.SECURITY_SERVICE_APP_AUTH_TOKEN_CONFIG,
            Configuration.FileRef.DELIMITER + appTokenFile.getAbsolutePath() + Configuration.FileRef.DELIMITER);

    try (OutputStream out = new FileOutputStream(new File(etcDir, "dpm-test.properties"))) {
        dpmProperties.store(out, null);
    }
    sparkProvider.copyDpmTokenIfRequired(sdcProperties, etcDir);
    try (InputStream in = new FileInputStream(new File(etcDir, "dpm-test.properties"))) {
        Properties gotProperties = new Properties();
        gotProperties.load(in);
        Assert.assertEquals("fooVal", gotProperties.getProperty("foo"));
        Assert.assertEquals("true", gotProperties.getProperty(RemoteSSOService.DPM_ENABLED));
        Assert.assertEquals(
                Configuration.FileRef.DELIMITER + ShellClusterProvider.CLUSTER_DPM_APP_TOKEN
                        + Configuration.FileRef.DELIMITER,
                gotProperties.getProperty(RemoteSSOService.SECURITY_SERVICE_APP_AUTH_TOKEN_CONFIG));
        List<String> gotLines = Files.readAllLines(
                new File(etcDir, ShellClusterProvider.CLUSTER_DPM_APP_TOKEN).toPath(),
                Charset.defaultCharset());
        Assert.assertEquals(1, gotLines.size());
        Assert.assertEquals("app-token-dummy-text", gotLines.get(0));
    }
    // dpm not enabled
    etcDir = tempFolder.newFolder();
    dpmProperties = new Properties();
    dpmProperties.setProperty("foo", "fooNewVal");
    dpmProperties.setProperty(RemoteSSOService.DPM_ENABLED, "false");
    try (OutputStream out = new FileOutputStream(new File(etcDir, "dpm-test.properties"))) {
        dpmProperties.store(out, null);
    }
    sparkProvider.copyDpmTokenIfRequired(sdcProperties, etcDir);
    try (InputStream in = new FileInputStream(new File(etcDir, "dpm-test.properties"))) {
        Properties gotProperties = new Properties();
        gotProperties.load(in);
        Assert.assertEquals("fooNewVal", gotProperties.getProperty("foo"));
        Assert.assertEquals("false", gotProperties.getProperty(RemoteSSOService.DPM_ENABLED));
    }
    // dpm enabled but token relative
    etcDir = tempFolder.newFolder();
    dpmProperties = new Properties();
    dpmProperties.setProperty("foo", "fooDpmEnabledTokenRelative");
    dpmProperties.setProperty(RemoteSSOService.DPM_ENABLED, "true");
    dpmProperties.setProperty(RemoteSSOService.SECURITY_SERVICE_APP_AUTH_TOKEN_CONFIG,
            Configuration.FileRef.DELIMITER + "relative_path_to_token.txt" + Configuration.FileRef.DELIMITER);
    try (OutputStream out = new FileOutputStream(new File(etcDir, "dpm-test.properties"))) {
        dpmProperties.store(out, null);
    }
    sparkProvider.copyDpmTokenIfRequired(sdcProperties, etcDir);
    try (InputStream in = new FileInputStream(new File(etcDir, "dpm-test.properties"))) {
        Properties gotProperties = new Properties();
        gotProperties.load(in);
        Assert.assertEquals("fooDpmEnabledTokenRelative", gotProperties.getProperty("foo"));
        Assert.assertEquals("true", gotProperties.getProperty(RemoteSSOService.DPM_ENABLED));
        Assert.assertEquals(
                Configuration.FileRef.DELIMITER + "relative_path_to_token.txt"
                        + Configuration.FileRef.DELIMITER,
                gotProperties.getProperty(RemoteSSOService.SECURITY_SERVICE_APP_AUTH_TOKEN_CONFIG));
    }
    // all configs in sdc.properties (similar to parcels)
    sdcProperties.remove(Configuration.CONFIG_INCLUDES);
    sdcProperties.setProperty(RemoteSSOService.DPM_ENABLED, "true");
    sdcProperties.setProperty(RemoteSSOService.SECURITY_SERVICE_APP_AUTH_TOKEN_CONFIG,
            Configuration.FileRef.DELIMITER + appTokenFile.getAbsolutePath() + Configuration.FileRef.DELIMITER);
    sparkProvider.copyDpmTokenIfRequired(sdcProperties, etcDir);
    Assert.assertEquals("true", sdcProperties.getProperty(RemoteSSOService.DPM_ENABLED));
    Assert.assertEquals(
            Configuration.FileRef.DELIMITER + ShellClusterProvider.CLUSTER_DPM_APP_TOKEN
                    + Configuration.FileRef.DELIMITER,
            sdcProperties.getProperty(RemoteSSOService.SECURITY_SERVICE_APP_AUTH_TOKEN_CONFIG));
}

From source file:com.codejumble.opentube.Main.java

private List<String> renderDownloadQueue(File importedFile) throws IOException {
    logger.error("Rendering the download page...");
    List<String> lines = Files.readAllLines(Paths.get(importedFile.getAbsolutePath()),
            Charset.defaultCharset());
    List<String> linesToRemove = new ArrayList<String>();
    for (String line : lines) {
        if (!urlValidator.isValid(line)) {
            linesToRemove.add(line);//from   w  ww  .  j  a  v  a2  s .  com
        }
    }
    lines.removeAll(linesToRemove);
    logger.error("Valid urls found: {}", lines.size());
    return lines;
}

From source file:org.apache.taverna.databundle.TestDataBundles.java

@Test
public void setErrorArgs() throws Exception {
    Path inputs = DataBundles.getInputs(dataBundle);
    Path portIn1 = DataBundles.getPort(inputs, "in1");
    Path errorPath = DataBundles.setError(portIn1, "Something did not work", "A very\n long\n error\n trace");
    assertEquals("in1.err", errorPath.getFileName().toString());

    List<String> errLines = Files.readAllLines(errorPath, Charset.forName("UTF-8"));
    assertEquals(6, errLines.size());//from w w w.j  a v a  2 s. co  m
    assertEquals("", errLines.get(0));
    assertEquals("Something did not work", errLines.get(1));
    assertEquals("A very", errLines.get(2));
    assertEquals(" long", errLines.get(3));
    assertEquals(" error", errLines.get(4));
    assertEquals(" trace", errLines.get(5));
}

From source file:com.facebook.buck.jvm.java.DefaultJavaLibraryIntegrationTest.java

@Test
public void testClassUsageFileOutput() throws IOException {
    setUpProjectWorkspaceForScenario("class_usage_file");

    // Run `buck build`.
    BuildTarget bizTarget = BuildTargetFactory.newInstance("//:biz");
    ProcessResult buildResult = workspace.runBuckCommand("build", bizTarget.getFullyQualifiedName());
    buildResult.assertSuccess("Successful build should exit with 0.");

    Path bizClassUsageFilePath = BuildTargetPaths.getGenPath(filesystem, bizTarget,
            "lib__%s__output/used-classes.json");

    List<String> lines = Files.readAllLines(workspace.getPath(bizClassUsageFilePath), UTF_8);

    assertEquals("Expected just one line of JSON", 1, lines.size());

    String utilJarPath;// w w w  .j a v  a 2 s.  c  om
    if (compileAgainstAbis.equals(TRUE)) {
        utilJarPath = MorePaths.pathWithPlatformSeparators("buck-out/gen/util#class-abi/util-abi.jar");
    } else {
        utilJarPath = MorePaths.pathWithPlatformSeparators("buck-out/gen/lib__util__output/util.jar");
    }
    String utilClassPath = MorePaths.pathWithPlatformSeparators("com/example/Util.class");

    JsonNode jsonNode = ObjectMappers.READER.readTree(lines.get(0));
    assertThat(jsonNode, new HasJsonField(utilJarPath,
            Matchers.equalTo(ObjectMappers.legacyCreate().valueToTree(new String[] { utilClassPath }))));
}

From source file:org.apache.taverna.databundle.TestDataBundles.java

@Test
public void setErrorCause() throws Exception {
    Path inputs = DataBundles.getInputs(dataBundle);
    Path portIn1 = DataBundles.getPort(inputs, "in1");
    Path cause1 = DataBundles.setError(portIn1, "Something did not work", "A very\n long\n error\n trace");
    Path portIn2 = DataBundles.getPort(inputs, "in2");
    Path cause2 = DataBundles.setError(portIn2, "Something else did not work", "Shorter trace");

    Path outputs = DataBundles.getOutputs(dataBundle);
    Path portOut1 = DataBundles.getPort(outputs, "out1");
    Path errorPath = DataBundles.setError(portOut1, "Errors in input", "", cause1, cause2);

    List<String> errLines = Files.readAllLines(errorPath, Charset.forName("UTF-8"));
    assertEquals("../inputs/in1.err", errLines.get(0));
    assertEquals("../inputs/in2.err", errLines.get(1));
    assertEquals("", errLines.get(2));
}

From source file:org.opencb.cellbase.app.transform.GeneParser.java

private Map<String, String> getGeneDescriptionMap() throws IOException {
    logger.info("Loading gene description data...");
    Map<String, String> geneDescriptionMap = new HashMap<>();
    String[] fields;//from ww  w.j  ava  2s .  c o  m
    if (geneDescriptionFile != null && Files.exists(geneDescriptionFile)
            && Files.size(geneDescriptionFile) > 0) {
        List<String> lines = Files.readAllLines(geneDescriptionFile, Charset.forName("ISO-8859-1"));
        //            List<String> lines = Files.readAllLines(geneDescriptionFile, Charset.defaultCharset());
        for (String line : lines) {
            fields = line.split("\t", -1);
            geneDescriptionMap.put(fields[0], fields[1]);
        }
    } else {
        logger.warn("Gene description file " + geneDescriptionFile + " not found");
        logger.warn("Gene description data not loaded");
    }
    return geneDescriptionMap;
}

From source file:org.archive.nutchwax.ImporterToHdfs.java

private List<String> getProcessedFiles(String path) {
    List<String> processed = new ArrayList<String>();

    try {//from  w ww.  ja v  a2s.  co  m
        java.nio.file.Path file = Paths.get(path);
        processed = Files.readAllLines(file, StandardCharsets.UTF_8);
    } catch (IOException e) {
        System.out.println("Fatal error: " + e);
        e.printStackTrace(System.out);
    }

    return processed;
}

From source file:com.opensmile.maven.EmoRecService.java

private String genderateHelp() {
    List<String> lines;
    String st = "";
    try {//from   www.  ja  v a2 s  . co m
        lines = Files.readAllLines(Paths.get(paths.getVar("REST_OPENSMILE") + "/general/help.txt"),
                Charset.defaultCharset());
        for (String line : lines) {
            st += line + "\n";
        }
    } catch (IOException e) {
        st = "";
    }
    return st;
}

From source file:org.apache.taverna.databundle.TestDataBundles.java

@Test
public void setErrorObj() throws Exception {
    Path inputs = DataBundles.getInputs(dataBundle);

    Path portIn1 = DataBundles.getPort(inputs, "in1");
    Path cause1 = DataBundles.setError(portIn1, "a", "b");
    Path portIn2 = DataBundles.getPort(inputs, "in2");
    Path cause2 = DataBundles.setError(portIn2, "c", "d");

    Path outputs = DataBundles.getOutputs(dataBundle);
    Path portOut1 = DataBundles.getPort(outputs, "out1");

    ErrorDocument error = new ErrorDocument();
    error.getCausedBy().add(cause1);/*  w  ww  . ja  v  a2  s. co  m*/
    error.getCausedBy().add(cause2);

    error.setMessage("Something did not work");
    error.setTrace("Here\nis\nwhy\n");

    Path errorPath = DataBundles.setError(portOut1, error);
    assertEquals("out1.err", errorPath.getFileName().toString());

    List<String> errLines = Files.readAllLines(errorPath, Charset.forName("UTF-8"));
    assertEquals(8, errLines.size());
    assertEquals("../inputs/in1.err", errLines.get(0));
    assertEquals("../inputs/in2.err", errLines.get(1));
    assertEquals("", errLines.get(2));
    assertEquals("Something did not work", errLines.get(3));
    assertEquals("Here", errLines.get(4));
    assertEquals("is", errLines.get(5));
    assertEquals("why", errLines.get(6));
    assertEquals("", errLines.get(7));
}