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:com.spotify.docker.BuildMojoTest.java

public void testBuildWithGeneratedDockerfileWithSquashCommands() throws Exception {
    final File pom = getTestFile("src/test/resources/pom-build-generated-dockerfile-with-squash-commands.xml");
    assertNotNull("Null pom.xml", pom);
    assertTrue("pom.xml does not exist", pom.exists());

    final BuildMojo mojo = setupMojo(pom);
    final DockerClient docker = mock(DockerClient.class);
    mojo.execute(docker);/*from   ww w . j ava 2s  .c  o m*/

    verify(docker).build(eq(Paths.get("target/docker")), eq("busybox"), any(AnsiProgressHandler.class));
    assertFilesCopied();
    assertEquals("wrong dockerfile contents", GENERATED_DOCKERFILE_WITH_SQUASH_COMMANDS,
            Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));
}

From source file:com.google.examples.JOSEToolBase.java

private String readFileAsUtf8String(String path) throws IOException {
    List<String> linelist = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
    String fileContent = StringUtils.join(linelist, "\n").trim();
    return fileContent;
}

From source file:org.jboss.as.test.integration.logging.formatters.XmlFormatterTestCase.java

private Collection<String> readLogLines() throws IOException {
    final Collection<String> result = new ArrayList<>();
    final StringBuilder sb = new StringBuilder();
    for (String line : Files.readAllLines(logFile, StandardCharsets.UTF_8)) {
        if (line.trim().isEmpty())
            continue;
        sb.append(line);/*w ww.j a va 2  s. c om*/
        if (line.endsWith("</record>")) {
            result.add(sb.toString());
            sb.setLength(0);
        }
    }
    return result;
}

From source file:rapture.series.file.FileSeriesStore.java

@Override
public List<SeriesValue> getPointsAfter(String key, String startColumn, String endColumn, int maxNumber) {
    File seriesFile = FileRepoUtils.makeGenericFile(parentDir, key + Parser.COLON_CHAR);
    if (!seriesFile.exists())
        return new ArrayList<>();
    if (!seriesFile.isFile())
        throw RaptureExceptionFactory
                .create("For FILE implementation you can't have a Series with the same name as a Folder");
    List<SeriesValue> series = new ArrayList<>();
    int limit = (maxNumber > overflowLimit) ? overflowLimit : maxNumber;
    boolean found = (startColumn == null);
    try {/*from w ww  .  j a  v a  2  s.co m*/
        List<String> lines = Files.readAllLines(seriesFile.toPath(), StandardCharsets.UTF_8);
        for (String line : lines) {
            char c = line.charAt(0);
            int i = line.indexOf(c, 1);
            String column = line.substring(1, i);
            byte[] bytes = line.substring(i + 1).getBytes();
            SeriesValue value = SeriesValueCodec.decode(column, bytes);
            if (!found) {
                if (!value.getColumn().equals(startColumn))
                    continue;
                found = true;
            }
            series.add(value);
            if (series.size() == limit)
                break;
            if ((endColumn != null) && value.getColumn().equals(endColumn))
                break;
        }
    } catch (IOException e) {
        log.debug(ExceptionToString.format(e));
        throw RaptureExceptionFactory.create("Cannot read Series " + key, e);
    }
    return series;
}

From source file:org.eclipse.winery.generators.ia.Generator.java

private void generateJavaFile(File javaService) throws IOException {

    // Generate methods
    StringBuilder sb = new StringBuilder();

    for (TOperation op : this.tinterface.getOperation()) {
        // Annotations
        sb.append("\t@WebMethod\n");
        sb.append("\t@SOAPBinding\n");
        sb.append("\t@Oneway\n");

        // Signatur
        String operationReturn = "void";
        sb.append("\tpublic " + operationReturn + " " + op.getName() + "(\n");

        // Parameter
        boolean first = true;
        if (op.getInputParameters() != null) {
            for (TParameter parameter : op.getInputParameters().getInputParameter()) {
                String parameterName = parameter.getName();

                if (first) {
                    first = false;/*from w ww .  ja va 2s .c  om*/
                    sb.append("\t\t");
                } else {
                    sb.append(",\n\t\t");
                }

                // Generate @WebParam
                sb.append("@WebParam(name=\"" + parameterName + "\", targetNamespace=\"" + this.namespace
                        + "\") ");

                // Handle required and optional parameters using @XmlElement
                if (parameter.getRequired().equals(TBoolean.YES)) {
                    sb.append("@XmlElement(required=true)");
                } else {
                    sb.append("@XmlElement(required=false)");
                }

                sb.append(" String " + parameterName);
            }
        }
        sb.append("\n\t) {\n");

        // If there are output parameters we generate the respective HashMap
        boolean outputParamsExist = (op.getOutputParameters() != null)
                && (!op.getOutputParameters().getOutputParameter().isEmpty());
        if (outputParamsExist) {
            sb.append("\t\t// This HashMap holds the return parameters of this operation.\n");
            sb.append("\t\tfinal HashMap<String,String> returnParameters = new HashMap<String, String>();\n\n");
        }

        sb.append("\t\t// TODO: Implement your operation here.\n");

        // Generate code to set output parameters
        if (outputParamsExist) {
            for (TParameter outputParam : op.getOutputParameters().getOutputParameter()) {
                sb.append("\n\n\t\t// Output Parameter '" + outputParam.getName() + "' ");
                if (outputParam.getRequired().equals(TBoolean.YES)) {
                    sb.append("(required)");
                } else {
                    sb.append("(optional)");
                }
                sb.append("\n\t\t// TODO: Set " + outputParam.getName() + " parameter here.");
                sb.append(
                        "\n\t\t// Do NOT delete the next line of code. Set \"\" as value if you want to return nothing or an empty result!");
                sb.append("\n\t\treturnParameters.put(\"" + outputParam.getName() + "\", \"TODO\");");
            }
            sb.append("\n\n\t\tsendResponse(returnParameters);\n");
        }

        sb.append("\t}\n\n");
    }

    // Read file and replace placeholders
    Charset cs = Charset.defaultCharset();
    List<String> lines = new ArrayList<>();
    for (String line : Files.readAllLines(javaService.toPath(), cs)) {
        // Replace web service method
        line = line.replaceAll(Generator.PLACEHOLDER_GENERATED_WEBSERVICE_METHODS, sb.toString());
        lines.add(line);
    }

    // Write file
    OpenOption[] options = new OpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING };
    Files.write(javaService.toPath(), lines, cs, options);
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.runtime.TestDockerContainerRuntime.java

@Test
public void testLaunchPrivilegedContainersEnabledAndUserInWhitelist()
        throws ContainerExecutionException, PrivilegedOperationException, IOException {
    //Enable privileged containers.
    conf.setBoolean(YarnConfiguration.NM_DOCKER_ALLOW_PRIVILEGED_CONTAINERS, true);
    //Add submittingUser to whitelist.
    conf.set(YarnConfiguration.NM_DOCKER_PRIVILEGED_CONTAINERS_ACL, submittingUser);

    DockerLinuxContainerRuntime runtime = new DockerLinuxContainerRuntime(mockExecutor);
    runtime.initialize(conf);//www . j  a  v  a2s. c  om

    env.put("YARN_CONTAINER_RUNTIME_DOCKER_RUN_PRIVILEGED_CONTAINER", "true");

    runtime.launchContainer(builder.build());
    PrivilegedOperation op = capturePrivilegedOperationAndVerifyArgs();
    List<String> args = op.getArguments();
    String dockerCommandFile = args.get(11);

    List<String> dockerCommands = Files.readAllLines(Paths.get(dockerCommandFile), Charset.forName("UTF-8"));

    Assert.assertEquals(1, dockerCommands.size());

    String command = dockerCommands.get(0);

    //submitting user is whitelisted. ensure --privileged is in the invocation
    Assert.assertTrue("Did not find expected '--privileged' in docker run args " + ": " + command,
            command.contains("--privileged"));
}

From source file:org.eclipse.winery.generators.ia.Generator.java

/**
 * Iterates recursively through all the files in the project working
 * directory and tries to replace the global placeholders.
 * //www .jav a  2s. c o  m
 * @param folderOrFile to start with
 */
private void updateFilesRecursively(File folderOrFile) {
    if (folderOrFile.isFile()) {

        if (folderOrFile.getAbsolutePath().endsWith(".jar")) {
            return;
        }

        Generator.logger.trace("Updating file " + folderOrFile);

        try {
            // Read file and replace placeholders
            Charset cs = Charset.defaultCharset();
            List<String> lines = new ArrayList<>();
            for (String line : Files.readAllLines(folderOrFile.toPath(), cs)) {
                line = line.replaceAll(Generator.PLACEHOLDER_CLASS_NAME, this.name);
                line = line.replaceAll(Generator.PLACEHOLDER_JAVA_PACKAGE, this.javaPackage);
                line = line.replaceAll(Generator.PLACEHOLDER_NAMESPACE, this.namespace);
                line = line.replaceAll(Generator.PLACEHOLDER_IA_ARTIFACT_TEMPLATE_UPLOAD_URL,
                        this.iaArtifactTemplateUploadUrl.toString());
                lines.add(line);
            }

            // Write file
            OpenOption[] options = new OpenOption[] { StandardOpenOption.WRITE,
                    StandardOpenOption.TRUNCATE_EXISTING };
            Files.write(folderOrFile.toPath(), lines, cs, options);

        } catch (IOException e) {
            e.printStackTrace();
        }

    } else {
        Generator.logger.trace("Updating folder " + folderOrFile);
        for (File childFile : folderOrFile.listFiles()) {
            this.updateFilesRecursively(childFile);
        }
    }
}

From source file:it.uniud.ailab.dcore.launchers.Launcher.java

/**
 * Load the document trying different charsets. The charset tried, are, in
 * order:/*  w ww .  j  a  va 2  s.co m*/
 * <ul>
 * <li>UTF-16;</li>
 * <li>UTF-8;</li>
 * <li>US-ASCII.</li>
 * </ul>
 *
 * @param filePath the path of the document
 * @return the text of the document
 * @throws IOException if the charset is not supported
 */
private static String loadDocument(File filePath) throws IOException {

    String document = "";

    IOException exception = null;
    // try different charsets. if none is recognized, throw the
    // exception detected when reading.
    try {
        document = String.join(" ", Files.readAllLines(filePath.toPath(), StandardCharsets.UTF_8));

    } catch (java.nio.charset.MalformedInputException e) {
        exception = e;
    }

    if (exception != null) {
        try {
            exception = null;
            document = String.join(" ", Files.readAllLines(filePath.toPath(), StandardCharsets.UTF_16));

        } catch (java.nio.charset.MalformedInputException e) {
            exception = e;
        }
    }

    if (exception != null) {
        try {
            exception = null;
            document = String.join(" ", Files.readAllLines(filePath.toPath(), StandardCharsets.US_ASCII));

        } catch (java.nio.charset.MalformedInputException e) {
            exception = e;
        }
    }

    // no charset has been recognized
    if (exception != null) {
        throw exception;
    }
    return document;
}

From source file:com.tunyk.mvn.plugins.htmlcompressor.HtmlCompressorMojo.java

@Override
public void execute() throws MojoExecutionException {
    if (!enabled) {
        getLog().info("HTML compression was turned off.");
        return;/*from ww  w. j  a v  a 2 s .  c o m*/
    }

    if (!new File(srcFolder).exists()) {
        getLog().warn("Compressor folder does not exist, skipping compression of " + srcFolder);
        return;
    }

    getLog().info("Compressing " + srcFolder);
    HtmlCompressor htmlCompressor = new HtmlCompressor(srcFolder, targetFolder);

    htmlCompressor.setFileExt(fileExt);
    htmlCompressor.setFileEncoding(encoding);
    htmlCompressor.setCreateJsonFile(javascriptHtmlSprite);
    htmlCompressor.setJsonIntegrationFilePath(javascriptHtmlSpriteIntegrationFile);
    htmlCompressor.setTargetJsonFilePath(javascriptHtmlSpriteTargetFile);

    com.googlecode.htmlcompressor.compressor.HtmlCompressor htmlCompressorHandler = new com.googlecode.htmlcompressor.compressor.HtmlCompressor();
    htmlCompressorHandler.setEnabled(enabled);
    htmlCompressorHandler.setRemoveComments(removeComments);
    htmlCompressorHandler.setRemoveMultiSpaces(removeMultiSpaces);
    htmlCompressorHandler.setRemoveIntertagSpaces(removeIntertagSpaces);
    htmlCompressorHandler.setRemoveQuotes(removeQuotes);
    htmlCompressorHandler.setSimpleDoctype(simpleDoctype);
    htmlCompressorHandler.setRemoveScriptAttributes(removeScriptAttributes);
    htmlCompressorHandler.setRemoveStyleAttributes(removeStyleAttributes);
    htmlCompressorHandler.setRemoveLinkAttributes(removeLinkAttributes);
    htmlCompressorHandler.setRemoveFormAttributes(removeFormAttributes);
    htmlCompressorHandler.setRemoveInputAttributes(removeInputAttributes);
    htmlCompressorHandler.setSimpleBooleanAttributes(simpleBooleanAttributes);
    htmlCompressorHandler.setRemoveJavaScriptProtocol(removeJavaScriptProtocol);
    htmlCompressorHandler.setRemoveHttpProtocol(removeHttpProtocol);
    htmlCompressorHandler.setRemoveHttpsProtocol(removeHttpsProtocol);
    htmlCompressorHandler.setCompressCss(compressCss);
    htmlCompressorHandler.setPreserveLineBreaks(preserveLineBreaks);
    htmlCompressorHandler.setYuiCssLineBreak(yuiCssLineBreak);
    htmlCompressorHandler.setCompressJavaScript(compressJavaScript);
    htmlCompressorHandler.setYuiJsNoMunge(yuiJsNoMunge);
    htmlCompressorHandler.setYuiJsPreserveAllSemiColons(yuiJsPreserveAllSemiColons);
    htmlCompressorHandler.setYuiJsLineBreak(yuiJsLineBreak);
    htmlCompressorHandler.setYuiJsDisableOptimizations(yuiJsDisableOptimizations);
    htmlCompressorHandler.setGenerateStatistics(generateStatistics);

    if (jsCompressor.equalsIgnoreCase("closure")) {
        ClosureJavaScriptCompressor closureCompressor = new ClosureJavaScriptCompressor();
        if (closureOptLevel != null
                && closureOptLevel.equalsIgnoreCase(ClosureJavaScriptCompressor.COMPILATION_LEVEL_ADVANCED)) {
            closureCompressor.setCompilationLevel(CompilationLevel.ADVANCED_OPTIMIZATIONS);
            closureCompressor.setCustomExternsOnly(closureCustomExternsOnly != null);
            if (closureExterns.length > 0) {
                List<SourceFile> externs = new ArrayList<SourceFile>();
                for (String externFile : closureExterns) {
                    externs.add(SourceFile.fromFile(externFile));
                }
                closureCompressor.setExterns(externs);
            }
        } else if (closureOptLevel != null
                && closureOptLevel.equalsIgnoreCase(ClosureJavaScriptCompressor.COMPILATION_LEVEL_WHITESPACE)) {
            closureCompressor.setCompilationLevel(CompilationLevel.WHITESPACE_ONLY);
        } else {
            closureCompressor.setCompilationLevel(CompilationLevel.SIMPLE_OPTIMIZATIONS);
        }

        htmlCompressorHandler.setJavaScriptCompressor(closureCompressor);
    }

    List<Pattern> preservePatternList = new ArrayList<Pattern>();
    boolean phpTagPatternAdded = false;
    boolean serverScriptTagPatternAdded = false;
    if (predefinedPreservePatterns != null) {
        for (String pattern : predefinedPreservePatterns) {
            if (!phpTagPatternAdded && pattern.equalsIgnoreCase("PHP_TAG_PATTERN")) {
                preservePatternList
                        .add(com.googlecode.htmlcompressor.compressor.HtmlCompressor.PHP_TAG_PATTERN);
                phpTagPatternAdded = true;
            } else if (!serverScriptTagPatternAdded && pattern.equalsIgnoreCase("SERVER_SCRIPT_TAG_PATTERN")) {
                preservePatternList
                        .add(com.googlecode.htmlcompressor.compressor.HtmlCompressor.SERVER_SCRIPT_TAG_PATTERN);
                serverScriptTagPatternAdded = true;
            }
        }
    }
    if (preservePatterns != null) {
        for (String preservePatternString : preservePatterns) {
            if (!preservePatternString.isEmpty()) {
                try {
                    preservePatternList.add(Pattern.compile(preservePatternString));
                } catch (PatternSyntaxException e) {
                    throw new MojoExecutionException(e.getMessage());
                }
            }
        }
    }
    if (preservePatternFiles != null) {
        for (File file : preservePatternFiles) {
            try {
                List<String> fileLines = Files.readAllLines(file.toPath(), Charset.forName(encoding));
                for (String line : fileLines) {
                    if (!line.isEmpty()) {
                        preservePatternList.add(Pattern.compile(line));
                    }
                }
            } catch (PatternSyntaxException e) {
                throw new MojoExecutionException(e.getMessage());
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage());
            }
        }
    }
    htmlCompressorHandler.setPreservePatterns(preservePatternList);
    htmlCompressor.setHtmlCompressor(htmlCompressorHandler);

    try {
        htmlCompressor.compress();
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }

    boolean si = true;

    // TODO: if no files matched pattern (*.htm or *.html) then this gives NullPointerException
    int origFilesizeBytes = htmlCompressor.getHtmlCompressor().getStatistics().getOriginalMetrics()
            .getFilesize();
    String origFilesize = FileTool.humanReadableByteCount(origFilesizeBytes, si);
    String origEmptyChars = String
            .valueOf(htmlCompressor.getHtmlCompressor().getStatistics().getOriginalMetrics().getEmptyChars());
    String origInlineEventSize = FileTool.humanReadableByteCount(
            htmlCompressor.getHtmlCompressor().getStatistics().getOriginalMetrics().getInlineEventSize(), si);
    String origInlineScriptSize = FileTool.humanReadableByteCount(
            htmlCompressor.getHtmlCompressor().getStatistics().getOriginalMetrics().getInlineScriptSize(), si);
    String origInlineStyleSize = FileTool.humanReadableByteCount(
            htmlCompressor.getHtmlCompressor().getStatistics().getOriginalMetrics().getInlineStyleSize(), si);

    int compFilesizeBytes = htmlCompressor.getHtmlCompressor().getStatistics().getCompressedMetrics()
            .getFilesize();
    String compFilesize = FileTool.humanReadableByteCount(compFilesizeBytes, si);
    String compEmptyChars = String
            .valueOf(htmlCompressor.getHtmlCompressor().getStatistics().getCompressedMetrics().getEmptyChars());
    String compInlineEventSize = FileTool.humanReadableByteCount(
            htmlCompressor.getHtmlCompressor().getStatistics().getCompressedMetrics().getInlineEventSize(), si);
    String compInlineScriptSize = FileTool.humanReadableByteCount(
            htmlCompressor.getHtmlCompressor().getStatistics().getCompressedMetrics().getInlineScriptSize(),
            si);
    String compInlineStyleSize = FileTool.humanReadableByteCount(
            htmlCompressor.getHtmlCompressor().getStatistics().getCompressedMetrics().getInlineStyleSize(), si);

    String elapsedTime = FileTool
            .getElapsedHMSTime(htmlCompressor.getHtmlCompressor().getStatistics().getTime());
    String preservedSize = FileTool
            .humanReadableByteCount(htmlCompressor.getHtmlCompressor().getStatistics().getPreservedSize(), si);
    Float compressionRatio = Float.valueOf(compFilesizeBytes) / Float.valueOf(origFilesizeBytes);
    Float spaceSavings = Float.valueOf(1) - compressionRatio;

    String format = "%-30s%-30s%-30s%-2s";
    NumberFormat formatter = new DecimalFormat("#0.00");
    String eol = "\n";
    String hr = "+-----------------------------+-----------------------------+-----------------------------+";
    StringBuilder sb = new StringBuilder("HTML compression statistics:").append(eol);
    sb.append(hr).append(eol);
    sb.append(String.format(format, "| Category", "| Original", "| Compressed", "|")).append(eol);
    sb.append(hr).append(eol);
    sb.append(String.format(format, "| Filesize", "| " + origFilesize, "| " + compFilesize, "|")).append(eol);
    sb.append(String.format(format, "| Empty Chars", "| " + origEmptyChars, "| " + compEmptyChars, "|"))
            .append(eol);
    sb.append(String.format(format, "| Script Size", "| " + origInlineScriptSize, "| " + compInlineScriptSize,
            "|")).append(eol);
    sb.append(
            String.format(format, "| Style Size", "| " + origInlineStyleSize, "| " + compInlineStyleSize, "|"))
            .append(eol);
    sb.append(String.format(format, "| Event Handler Size", "| " + origInlineEventSize,
            "| " + compInlineEventSize, "|")).append(eol);
    sb.append(hr).append(eol);
    sb.append(String.format("%-90s%-2s",
            String.format("| Time: %s, Preserved: %s, Compression Ratio: %s, Savings: %s%%", elapsedTime,
                    preservedSize, formatter.format(compressionRatio), formatter.format(spaceSavings * 100)),
            "|")).append(eol);
    sb.append(hr).append(eol);

    String statistics = sb.toString();
    getLog().info(statistics);
    try {
        FileUtils.writeStringToFile(new File(htmlCompressionStatistics), statistics, encoding);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage());
    }

    getLog().info("HTML compression completed.");
}

From source file:com.splicemachine.derby.impl.load.HdfsImportIT.java

@Test
public void testImportMultiFilesPKViolations() throws Exception {
    try (PreparedStatement ps = methodWatcher.prepareStatement(format("call SYSCS_UTIL.IMPORT_DATA(" + "'%s'," + // schema name
            "'%s'," + // table name
            "null," + // insert column list
            "'%s'," + // file path
            "','," + // column delimiter
            "null," + // character delimiter
            "null," + // timestamp format
            "null," + // date format
            "null," + // time format
            "-1," + // max bad records
            "'%s'," + // bad record dir
            "'true'," + // has one line records
            "null)", // char set
            spliceSchemaWatcher.schemaName, multiPK.tableName, getResourceDirectory() + "/multiFilePKViolation",
            BADDIR.getCanonicalPath()))) {
        try (ResultSet rs = ps.executeQuery()) {
            assertTrue(rs.next());/* w w  w.  j  ava  2  s  . c  om*/

            // TODO SPLICE-1177 check for exact number of failed rows
            //                int failed = rs.getInt(2);
            //                assertEquals("Failed rows don't match", 4, failed);

            boolean exists = existsBadFile(BADDIR, "multiFilePKViolation.bad");
            List<String> badFiles = getAllBadFiles(BADDIR, "multiFilePKViolation.bad");
            assertTrue("Bad file " + badFiles + " does not exist.", exists);
            List<String> badLines = new ArrayList<>();
            for (String badFile : badFiles) {
                badLines.addAll(
                        Files.readAllLines((new File(BADDIR, badFile)).toPath(), Charset.defaultCharset()));
            }
            // TODO SPLICE-1177 expect exactly 4 lines
            assertTrue("Expected some lines in bad files " + badFiles, badLines.size() > 0);
        }
    }
    try (ResultSet rs = methodWatcher.executeQuery("select count(*) from " + multiPK)) {
        Assert.assertTrue("Did not return a row!", rs.next());
        long c = rs.getLong(1);
        Assert.assertEquals("Incorrect row count!", 9, c);
        Assert.assertFalse("Returned too many rows!", rs.next());
    }
}