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:neembuu.uploader.v2tov3conversion.ConvertUploaderClass.java

public ConvertUploaderClass(Path in) throws IOException {
    String myClassName = in.getFileName().toString();
    myClassName = myClassName.substring(0, myClassName.indexOf("."));
    this.myClassName = myClassName;
    is = Files.readAllLines(in, Charset.defaultCharset());
}

From source file:org.sonar.plugins.groovy.codenarc.apt.AptParser.java

private Map<String, AptResult> readFile(File file) throws Exception {
    Map<String, AptResult> results = Maps.newHashMap();
    List<String> lines = Files.readAllLines(file.toPath(), Charset.forName("UTF-8"));

    boolean inRule = false;
    boolean inParameters = false;
    boolean inExample = false;
    boolean inDescription = false;
    String currentRule = null;/*  w  w  w  .j  av  a2 s.  co m*/
    AptResult currentResult = null;
    RuleParameter currentParameter = null;
    int[] splits = new int[3];
    for (int index = 0; index < lines.size(); index++) {
        String fullLine = lines.get(index);
        String line = fullLine.trim();
        if (line.startsWith(NEW_RULE_PREFIX) && !line.startsWith(LIST_PREFIX) && inRule) {
            results.put(currentRule, currentResult);
            inRule = false;
            inDescription = false;
            currentRule = null;
            currentResult = null;
        }
        if (!inRule && line.startsWith(NEW_RULE_PREFIX)) {
            currentRule = getRuleName(line.trim());
            inRule = currentRule != null;
            if (inRule) {
                currentResult = results.get(currentRule);
                if (currentResult == null) {
                    currentResult = new AptResult(currentRule);
                }
            }
        } else if (inRule && !inExample && isExampleSeparator(line)) {
            inExample = true;
            inDescription = false;
            currentResult.description += "<pre>\n";
        } else if (inRule && !inExample && !inDescription && !inParameters && isValidDescriptionLine(line)) {
            inDescription = true;
            if (StringUtils.isNotBlank(line)) {
                currentResult.description += "<p>" + line;
            }
        } else if (inRule && !inExample && inDescription && !inParameters
                && !currentResult.description.endsWith("</pre>\n") && isValidDescriptionLine(line)) {
            if (isEndOfParagraph(currentResult, line)) {
                currentResult.description += "</p>\n";
            } else {
                currentResult.description += getParagraphLine(currentResult, line);
            }
        } else if (inRule && inExample && isExampleSeparator(line)) {
            inExample = false;
            inDescription = true;
            currentResult.description += "</pre>\n";
        } else if (inRule && inExample) {
            currentResult.description += fullLine + "\n";
        } else if (inRule && !inParameters && line.matches(PARAMETER_START_SEPARATOR)) {
            inDescription = false;
            inParameters = true;
            currentParameter = new RuleParameter();
            splits[0] = line.indexOf('*') + 1;
            splits[1] = line.indexOf('+', splits[0]) + 1;
            splits[2] = line.indexOf('+', splits[1]) + 1;
        } else if (inRule && inParameters && (line.matches(PARAMETER_CONTENT))) {
            String[] blocks = new String[3];
            blocks[0] = line.substring(splits[0], splits[1] - 1);
            blocks[1] = line.substring(splits[1], splits[2] - 1);
            blocks[2] = line.substring(splits[2], line.length() - 1);
            if (blocks.length == 3 && !isHeaderLine(blocks)) {
                String key = blocks[0].trim();
                String description = blocks[1].trim();
                String defaultValue = blocks[2].trim();
                if (StringUtils.isNotBlank(key)) {
                    currentParameter.setKey(currentParameter.getKey() + key.replaceAll("(-)+", ""));
                }
                if (StringUtils.isNotBlank(defaultValue) && !currentParameter.hasDefaultValue()) {
                    currentParameter.setDefaultValue(cleanDefaultValue(defaultValue));
                }
                if (StringUtils.isNotBlank(description)) {
                    currentParameter.setDescription(
                            currentParameter.getDescription() + cleanDescription(description, true) + " ");
                }
            }
        } else if (inRule && inParameters && isParameterSeparator(line)) {
            if (!currentParameter.isEmpty()) {
                currentResult.parameters.add(currentParameter);
            }
            currentParameter = new RuleParameter();
        } else if (inRule && inParameters) {
            if (!currentParameter.isEmpty()) {
                currentResult.parameters.add(currentParameter);
            }
            currentParameter = new RuleParameter();
            inParameters = false;
            inDescription = true;
        }
    }

    // last item
    if (inRule) {
        results.put(currentRule, currentResult);
    }

    return results;
}

From source file:org.sonar.plugins.gosu.codenarc.apt.AptParser.java

private Map<String, AptResult> readFile(File file) throws Exception {
    Map<String, AptResult> results = Maps.newHashMap();
    List<String> lines = Files.readAllLines(file.toPath(), Charset.forName("UTF-8"));

    boolean inRule = false;
    boolean inParameters = false;
    boolean inExample = false;
    boolean inDescription = false;
    String currentRule = null;/* w  w w.  ja v a  2  s  . com*/
    AptResult currentResult = null;
    RuleParameter currentParameter = null;
    int[] splits = new int[3];
    for (int index = 0; index < lines.size(); index++) {
        String fullLine = lines.get(index);
        String line = fullLine.trim();
        if (line.startsWith(NEW_RULE_PREFIX) && !line.startsWith(LIST_PREFIX) && inRule) {
            results.put(currentRule, currentResult);
            inRule = false;
            inDescription = false;
            currentRule = null;
            currentResult = null;
        }
        if (!inRule && line.startsWith(NEW_RULE_PREFIX)) {
            currentRule = getRuleName(line.trim());
            inRule = currentRule != null;
            if (inRule) {
                currentResult = results.get(currentRule);
                if (currentResult == null) {
                    currentResult = new AptResult(currentRule);
                }
            }
        } else if (inRule && !inExample && isExampleSeparator(line)) {
            inExample = true;
            inDescription = false;
            currentResult.description += "<pre>\n";
        } else if (inRule && !inExample && !inDescription && !inParameters && isValidDescriptionLine(line)) {
            inDescription = true;
            if (StringUtils.isNotBlank(line)) {
                currentResult.description += "<p>" + line;
            }
        } else if (inRule && !inExample && inDescription && !inParameters
                && !currentResult.description.endsWith("</pre>\n") && isValidDescriptionLine(line)) {
            if (isEndOfParagraph(currentResult, line)) {
                currentResult.description += "</p>\n";
            } else {
                currentResult.description += getParagraphLine(currentResult, line);
            }
        } else if (inRule && inExample && isExampleSeparator(line)) {
            inExample = false;
            inDescription = true;
            currentResult.description += "</pre>\n";
        } else if (inRule && inExample) {
            currentResult.description += fullLine + "\n";
        } else if (inRule && !inParameters && line.matches(PARAMETER_START_SEPARATOR)) {
            inDescription = false;
            inParameters = true;
            currentParameter = new RuleParameter();
            splits[0] = line.indexOf('*') + 1;
            splits[1] = line.indexOf('+', splits[0]) + 1;
            splits[2] = line.indexOf('+', splits[1]) + 1;
        } else if (inRule && inParameters && (line.matches(PARAMETER_CONTENT))) {
            String[] blocks = new String[3];
            blocks[0] = line.substring(splits[0], splits[1] - 1);
            blocks[1] = line.substring(splits[1], splits[2] - 1);
            blocks[2] = line.substring(splits[2], line.length() - 1);
            if (blocks.length == 3 && !isHeaderLine(blocks)) {
                String key = blocks[0].trim();
                String description = blocks[1].trim();
                String defaultValue = blocks[2].trim();
                if (StringUtils.isNotBlank(key)) {
                    currentParameter.key = currentParameter.key + key.replaceAll("(-)+", "");
                }
                if (StringUtils.isNotBlank(defaultValue) && !currentParameter.hasDefaultValue()) {
                    currentParameter.defaultValue = cleanDefaultValue(defaultValue);
                }
                if (StringUtils.isNotBlank(description)) {
                    currentParameter.description = currentParameter.description
                            + cleanDescription(description, true) + " ";
                }
            }
        } else if (inRule && inParameters && isParameterSeparator(line)) {
            if (!currentParameter.isEmpty()) {
                currentResult.parameters.add(currentParameter);
            }
            currentParameter = new RuleParameter();
        } else if (inRule && inParameters) {
            if (!currentParameter.isEmpty()) {
                currentResult.parameters.add(currentParameter);
            }
            currentParameter = new RuleParameter();
            inParameters = false;
            inDescription = true;
        }
    }

    // last item
    if (inRule) {
        results.put(currentRule, currentResult);
    }

    return results;
}

From source file:edu.usu.sdl.openstorefront.usecase.AttributeImport.java

public Map<String, AttributeTypeView> loadAttributeMap() {
    Map<String, AttributeTypeView> attributeMap = new HashMap<>();

    CSVParser parser = new CSVParser();
    Path path = Paths.get(FileSystemManager.getDir(FileSystemManager.IMPORT_DIR) + "/attributes.csv");
    try {/* www.  j a va 2 s.  co m*/

        List<String> lines = Files.readAllLines(path, Charset.defaultCharset());
        lines.forEach(line -> {
            if (StringUtils.isNotBlank(line)) {
                if (line.toLowerCase().startsWith("attribute type") == false) {
                    try {
                        String data[] = parser.parseLine(line);
                        String type = data[0].toUpperCase().trim();
                        AttributeTypeView attributeTypeView = null;
                        if (attributeMap.containsKey(type)) {
                            attributeTypeView = attributeMap.get(type);
                        } else {
                            attributeTypeView = new AttributeTypeView();
                            attributeTypeView.setAttributeType(data[0].trim());
                            attributeTypeView.setDescription(data[1].trim());
                            attributeTypeView.setArchitectureFlg(Convert.toBoolean(data[2].trim()));
                            attributeTypeView.setVisibleFlg(Convert.toBoolean(data[3].trim()));
                            attributeTypeView.setImportantFlg(Convert.toBoolean(data[4].trim()));
                            attributeTypeView.setRequiredFlg(Convert.toBoolean(data[5].trim()));
                            attributeTypeView.setAllowMultipleFlg(Convert.toBoolean(data[6].trim()));

                            attributeMap.put(type, attributeTypeView);
                        }
                        AttributeCodeView attributeCodeView = new AttributeCodeView();
                        attributeCodeView.setCode(data[7].toUpperCase().trim());
                        attributeCodeView.setLabel(data[8].trim());

                        if (data.length > 9) {
                            attributeCodeView.setDescription(data[9].trim());
                        }
                        attributeTypeView.getCodes().add(attributeCodeView);

                    } catch (IOException ex) {
                        log.log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    } catch (IOException ex) {
        log.log(Level.SEVERE, null, ex);
    }

    //load archtec
    AttributeTypeView archView = loadDI2EArch();
    attributeMap.put(archView.getAttributeType(), archView);

    archView = loadJCFSLArch();
    attributeMap.put(archView.getAttributeType(), archView);

    archView = loadJCAArch();
    attributeMap.put(archView.getAttributeType(), archView);

    archView = loadJARMESArch();
    attributeMap.put(archView.getAttributeType(), archView);

    return attributeMap;
}

From source file:com.huawei.streaming.cql.CQLTestCommons.java

/**
 * ??/*from www  .j a v  a  2s .  co m*/
 *
 * @param f1 1
 * @param f2 2
 * @return ?true
 * @throws IOException ?
 */
public static boolean compareFileContent(File f1, File f2) throws IOException {
    List<String> f1Contexts = Files.readAllLines(f1.toPath(), CHARSET);
    List<String> f2Contexts = Files.readAllLines(f2.toPath(), CHARSET);

    if (f1Contexts.size() != f2Contexts.size()) {
        return false;
    }
    for (int i = 0; i < f1Contexts.size(); i++) {
        String s1 = f1Contexts.get(i).trim();
        String s2 = f2Contexts.get(i).trim();
        if (Strings.isNullOrEmpty(s1) && Strings.isNullOrEmpty(s2)) {
            continue;
        }

        if (!s1.equals(s2)) {
            return false;
        }
    }
    return true;
}

From source file:adalid.util.io.FileWrapper.java

public List<String> read() {
    String fileName = file.getName();
    String fileType = StringUtils.substringAfterLast(fileName, ".");
    if (StringUtils.isBlank(fileType)) {
        fileType = fileName;// w w  w.j a v  a  2s  .  com
    } else {
        fileType = fileType.toLowerCase();
    }
    type = "binary";
    charset = null;
    for (Charset cs : STANDARD_CHARSETS) {
        try {
            list = Files.readAllLines(path, cs);
            if (isEmpty()) {
                readingWarnings++;
                logger.warn(file.getPath() + " is empty ");
            }
            type = "text/" + fileType;
            charset = cs;
            return list;
        } catch (IOException ex) {
            readingWarnings++;
            logger.warn(ex);
            logger.warn(file.getPath() + " could not be read using " + cs);
        }
    }
    readingErrors++;
    logger.error(file.getPath() + " could not be read using a standard charset ");
    return null;
}

From source file:ml.shifu.core.util.CommonUtils.java

public static List<String> readAllLines(String path) throws IOException {
    return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
}

From source file:com.cloudera.dataflow.spark.TransformTranslatorTest.java

/**
 * Builds a simple pipeline with TextIO.Read and TextIO.Write, runs the pipeline
 * in DirectPipelineRunner and on SparkPipelineRunner, with the mapped dataflow-to-spark
 * transforms. Finally it makes sure that the results are the same for both runs.
 *//*from  www.j av  a2  s.  c  om*/
@Test
public void testTextIOReadAndWriteTransforms() throws IOException {
    String directOut = runPipeline("direct", directRunner);
    String sparkOut = runPipeline("spark", sparkRunner);

    List<String> directOutput = Files.readAllLines(Paths.get(directOut + "-00000-of-00001"), Charsets.UTF_8);

    List<String> sparkOutput = Files.readAllLines(Paths.get(sparkOut + "-00000-of-00001"), Charsets.UTF_8);

    // sort output to get a stable result (PCollections are not ordered)
    Collections.sort(directOutput);
    Collections.sort(sparkOutput);

    Assert.assertArrayEquals(directOutput.toArray(), sparkOutput.toArray());
}

From source file:org.geppetto.persistence.db.OSGiLocalPersistenceManagerFactoryBean.java

public OSGiLocalPersistenceManagerFactoryBean() {
    File dbConnFile = new File(PathConfiguration.settingsFolder + "/db.properties");
    try {/*from   w ww.  ja v  a  2 s. c o  m*/
        List<String> lines = Files.readAllLines(dbConnFile.toPath(), Charset.defaultCharset());
        for (String line : lines) {
            int eqIndex = line.indexOf("=");
            if (!line.startsWith("#") && eqIndex > 0) {
                dbConnProperties.put(line.substring(0, eqIndex), line.substring(eqIndex + 1));
            }
        }
    } catch (IOException e) {
        _logger.warn("Could not read DB connection file", e);
    }
}

From source file:com.javacreed.api.secureproperties.spring.SpringPropertiesTest.java

@Test
public void test() throws IOException {

    final String[] configurations = { "test-configuration-1.xml", "test-configuration-2.xml",
            "test-configuration-3.xml", "test-configuration-4.xml", "test-configuration-5.xml",
            /* "test-configuration-6.xml" */ };

    /*/*  ww  w . jav  a 2s.c om*/
     * The source properties file that contains the plain text password and the target properties file from where the
     * configuration reads the properties
     */
    final File source = new File("src/test/resources/samples/properties/file.001.properties");
    final File target = new File("target/samples/properties/file.001.properties");
    target.getParentFile().mkdirs();

    for (final String configuration : configurations) {

        // Copy the properties file before the test
        Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);

        for (int i = 0; i < 3; i++) {
            try (ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    "/spring/" + configuration)) {
                // Check all three properties
                Assert.assertEquals("Albert",
                        applicationContext.getBeanFactory().resolveEmbeddedValue("${name}"));
                Assert.assertEquals("Somewhere in Malta",
                        applicationContext.getBeanFactory().resolveEmbeddedValue("${address}"));
                Assert.assertEquals("my long secret password",
                        applicationContext.getBeanFactory().resolveEmbeddedValue("${password}"));

                // Make sure that the password was encoded
                final List<String> lines = Files.readAllLines(target.toPath(), Charset.forName("UTF-8"));
                Assert.assertEquals(4, lines.size());
                Assert.assertEquals("# This is a simple properties file", lines.get(0));
                Assert.assertEquals("name=Albert", lines.get(1));
                Assert.assertEquals("address=Somewhere in Malta", lines.get(2));
                Assert.assertTrue(lines.get(3).startsWith("password={enc}"));
                Assert.assertFalse(lines.get(3).contains("my long secret password"));
            }
        }
    }
}