Example usage for java.nio.file Files newBufferedReader

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

Introduction

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

Prototype

public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException 

Source Link

Document

Opens a file for reading, returning a BufferedReader that may be used to read text from the file in an efficient manner.

Usage

From source file:org.wso2.carbon.pc.core.transfer.ProcessImport.java

/**
 * Add the process text of the imported process into the registry
 *
 * @param processName process name/*w w w  .ja va2  s . c o m*/
 * @param processVersion process version
 * @param processDirPath process directory path
 * @param processAssetPath process path
 * @throws IOException
 * @throws RegistryException
 */
private void setProcessText(String processName, String processVersion, String processDirPath,
        String processAssetPath) throws IOException, RegistryException {
    Path processTextFilePath = Paths
            .get(processDirPath + "/" + ProcessCenterConstants.EXPORTED_PROCESS_TEXT_FILE);
    if (Files.exists(processTextFilePath)) {
        String processTextFileContent = null;
        try (BufferedReader reader = Files.newBufferedReader(processTextFilePath, Charsets.US_ASCII)) {
            String line;
            while ((line = reader.readLine()) != null) {
                processTextFileContent += line;
            }
        } catch (IOException e) {
            String errMsg = "Error in reading process tags file";
            throw new IOException(errMsg, e);
        }

        // store process text as a separate resource
        String processTextResourcePath = ProcessCenterConstants.PROCESS_TEXT_PATH + processName + "/"
                + processVersion;
        if (processTextFileContent != null && processTextFileContent.length() > 0) {
            Resource processTextResource = reg.newResource();
            processTextResource.setContent(processTextFileContent);
            processTextResource.setMediaType(MediaType.TEXT_HTML);
            reg.put(processTextResourcePath, processTextResource);
            reg.addAssociation(processTextResourcePath, processAssetPath,
                    ProcessCenterConstants.ASSOCIATION_TYPE);
        }
    }
}

From source file:eu.itesla_project.eurostag.EurostagImpactAnalysis.java

private void readSecurityIndexes(List<Contingency> contingencies, Path workingDir, ImpactAnalysisResult result)
        throws IOException {
    long start = System.currentTimeMillis();
    int files = 0;

    for (int i = 0; i < contingencies.size(); i++) {
        Contingency contingency = contingencies.get(i);
        for (String securityIndexFileName : Arrays.asList(TSO_LIMITS_SECURITY_INDEX_FILE_NAME,
                WP43_SMALLSIGNAL_SECURITY_INDEX_FILE_NAME, WP43_TRANSIENT_SECURITY_INDEX_FILE_NAME,
                WP43_OVERLOAD_SECURITY_INDEX_FILE_NAME, WP43_UNDEROVERVOLTAGE_SECURITY_INDEX_FILE_NAME)) {
            Path file = workingDir.resolve(
                    securityIndexFileName.replace(Command.EXECUTION_NUMBER_PATTERN, Integer.toString(i)));
            if (Files.exists(file)) {
                try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
                    for (SecurityIndex index : SecurityIndexParser.fromXml(contingency.getId(), reader)) {
                        result.addSecurityIndex(index);
                    }/*www  .j  av a2  s  .co m*/
                }
                files++;
            }
        }
        // also scan errors in output
        EurostagUtil.searchErrorMessage(
                workingDir.resolve(
                        FAULT_OUT_GZ_FILE_NAME.replace(Command.EXECUTION_NUMBER_PATTERN, Integer.toString(i))),
                result.getMetrics(), i);
    }

    LOGGER.trace("{} security indexes files read in {} ms", files, (System.currentTimeMillis() - start));
}

From source file:org.opencb.cellbase.app.cli.DownloadCommandExecutor.java

private String getUniProtRelease(String relnotesFilename) {
    Path path = Paths.get(relnotesFilename);
    Files.exists(path);//from   w  w  w.  ja v a  2s. c o m
    try {
        // The first line at the relnotes.txt file contains the UniProt release
        BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());
        String release = reader.readLine().split(" ")[2];
        reader.close();
        return release;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.wso2.carbon.pc.core.transfer.ProcessImport.java

/**
 * Add the process tags of the imported process to registry
 *
 * @param processDirPath process directory path
 * @param processAssetPath process path//from   w w w.j av  a  2s  . c  o  m
 * @throws IOException
 * @throws RegistryException
 */
private void setProcessTags(String processDirPath, String processAssetPath)
        throws IOException, RegistryException {
    Path processTagsFilePath = Paths.get(processDirPath + "/" + ProcessCenterConstants.PROCESS_TAGS_FILE);
    if (Files.exists(processTagsFilePath)) {
        String tagsFileContent = "";
        try (BufferedReader reader = Files.newBufferedReader(processTagsFilePath, Charsets.US_ASCII)) {
            String line;
            while ((line = reader.readLine()) != null) {
                tagsFileContent += line;
            }
        } catch (IOException e) {
            String errMsg = "Error in reading process tags file";
            throw new IOException(errMsg, e);
        }

        String[] tags = tagsFileContent.split(ProcessCenterConstants.TAGS_FILE_TAG_SEPARATOR);
        for (String tag : tags) {
            if (tag.length() > 0) {
                reg.applyTag(processAssetPath, tag);
            }
        }
    }
}

From source file:popgenutils.dfcp.PrepareVCF4DFCP.java

/**
 * /*from   w  w w. j  a v a 2 s. co  m*/
 */
private void split() {
    StringBuilder header = new StringBuilder();
    LineBuilder lines = new LineBuilder(window_size);
    try (BufferedReader in = Files.newBufferedReader(Paths.get(filename), Charset.forName("UTF-8"))) {
        int cnt = 0;
        int filecnt = 0;
        String line = null;
        while ((line = in.readLine()) != null) {
            if (line.startsWith("#")) {
                header.append(line + System.getProperty("line.separator"));
            } else {
                cnt++;
                lines.addLine(line);
                if (cnt == window_size) {
                    cnt = overlap_size;
                    lines.printFile(header.toString(), output_dir + filename.substring(0, filename.indexOf('.'))
                            + "_" + window_size + "_" + overlap_size + "_" + (filecnt++) + ".vcf");
                }
            }
        }
    } catch (IOException e) {
        System.err.println("could not read from file " + filename);
        e.printStackTrace();
    }
}

From source file:org.opencb.cellbase.app.cli.DownloadCommandExecutor.java

private String getDisgenetVersion(Path path) {
    Files.exists(path);//from   w  ww  . j  av a 2  s  .c  om
    try {
        BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset());
        String line = reader.readLine();
        // There shall be a line at the README.txt containing the version.
        // e.g. The files in the current directory contain the data corresponding to the latest release (version 4.0, April 2016). ...
        while (line != null) {
            if (line.contains("(version")) {
                String version = line.split("\\(")[1].split("\\)")[0];
                reader.close();
                return version;
            }
            line = reader.readLine();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.asterix.extension.grammar.GrammarExtensionMojo.java

private void processExtension() throws MojoExecutionException {
    try (BufferedReader reader = Files.newBufferedReader(Paths.get(base, gextension), StandardCharsets.UTF_8)) {
        StringBuilder identifier = new StringBuilder();
        String nextOperation = OVERRIDEPRODUCTION;
        while (read || (position.line = reader.readLine()) != null) {
            read = false;//w w  w . j a va2  s .com
            if (position.line.trim().startsWith("//")) {
                // skip comments
                continue;
            }
            String[] tokens = position.line.split(REGEX_WS_PAREN);
            position.index = 0;
            int openBraceIndex = position.line.indexOf(OPEN_BRACE);
            int openAngularIndex = position.line.trim().indexOf(OPEN_ANGULAR);
            if (tokens.length > 0 && identifier.length() == 0 && EXTENSIONKEYWORDS.contains(tokens[0])) {
                switch (tokens[0]) {
                case KWIMPORT:
                    handleImport(reader);
                    break;
                case KWUNIMPORT:
                    handleUnImport(reader);
                    break;
                case NEWPRODUCTION:
                    nextOperation = NEWPRODUCTION;
                    break;
                case MERGEPRODUCTION:
                    nextOperation = MERGEPRODUCTION;
                    shouldReplace = shouldReplace(tokens);
                    break;
                case OVERRIDEPRODUCTION:
                    nextOperation = OVERRIDEPRODUCTION;
                    break;
                default:
                    break;
                }
            } else if (openBraceIndex >= 0 && openAngularIndex < 0) {
                String beforeBrace = position.line.substring(0, openBraceIndex);
                if (beforeBrace.trim().length() > 0) {
                    identifier.append(beforeBrace);
                } else if (identifier.length() == 0) {
                    identifier.append(lastIdentifier);
                }
                position.index = openBraceIndex;
                switch (nextOperation) {
                case NEWPRODUCTION:
                    handleNew(identifier, reader);
                    break;
                case OVERRIDEPRODUCTION:
                    handleOverride(identifier, reader);
                    break;
                case MERGEPRODUCTION:
                    handleMerge(identifier, reader);
                    break;
                default:
                    throw new MojoExecutionException("Malformed extention file");
                }
                nextOperation = NEWPRODUCTION;
            } else if (openAngularIndex == 0) {
                if (nextOperation != NEWPRODUCTION) {
                    throw new MojoExecutionException("Can only add new REGEX production kind");
                }
                position.index = position.line.indexOf(OPEN_ANGULAR);
                readFinalProduction(identifier, reader);
                addFinalProduction(identifier, extensionFinals);
            } else if (identifier.length() > 0 || position.line.trim().length() > 0) {
                identifier.append(position.line);
                identifier.append('\n');
            }
        }
    } catch (Exception e) {
        getLog().error(e);
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

From source file:org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryStorage.java

@Override
public void applyServerSideRelocation(ManagedRepositoryContent managedRepository, ArtifactReference artifact)
        throws ProxyDownloadException {
    if ("pom".equals(artifact.getType())) {
        return;/*  ww  w .ja  v  a2s  . com*/
    }

    // Build the artifact POM reference
    ArtifactReference pomReference = new ArtifactReference();
    pomReference.setGroupId(artifact.getGroupId());
    pomReference.setArtifactId(artifact.getArtifactId());
    pomReference.setVersion(artifact.getVersion());
    pomReference.setType("pom");

    RepositoryProxyConnectors connectors = applicationContext.getBean("repositoryProxyConnectors#default",
            RepositoryProxyConnectors.class);

    // Get the artifact POM from proxied repositories if needed
    connectors.fetchFromProxies(managedRepository, pomReference);

    // Open and read the POM from the managed repo
    File pom = managedRepository.toFile(pomReference);

    if (!pom.exists()) {
        return;
    }

    try {
        // MavenXpp3Reader leaves the file open, so we need to close it ourselves.

        Model model = null;
        try (Reader reader = Files.newBufferedReader(pom.toPath(), Charset.defaultCharset())) {
            model = MAVEN_XPP_3_READER.read(reader);
        }

        DistributionManagement dist = model.getDistributionManagement();
        if (dist != null) {
            Relocation relocation = dist.getRelocation();
            if (relocation != null) {
                // artifact is relocated : update the repositoryPath
                if (relocation.getGroupId() != null) {
                    artifact.setGroupId(relocation.getGroupId());
                }
                if (relocation.getArtifactId() != null) {
                    artifact.setArtifactId(relocation.getArtifactId());
                }
                if (relocation.getVersion() != null) {
                    artifact.setVersion(relocation.getVersion());
                }
            }
        }
    } catch (IOException e) {
        // Unable to read POM : ignore.
    } catch (XmlPullParserException e) {
        // Invalid POM : ignore
    }
}

From source file:de.qucosa.webapi.v1.DocumentResourceFileTest.java

private void assertFileEquals(File expected, File actual) throws IOException {
    String s1 = IOUtils.toString(Files.newBufferedReader(expected.toPath(), Charset.defaultCharset())).trim();
    String s2 = IOUtils.toString(Files.newBufferedReader(actual.toPath(), Charset.defaultCharset())).trim();
    if (!s1.equals(s2))
        fail("File contents are not equal");
}

From source file:org.opencb.cellbase.app.cli.DownloadCommandExecutor.java

private String getLine(Path readmePath, int lineNumber) {
    Files.exists(readmePath);// w  w w.ja v a 2  s . c o  m
    try {
        BufferedReader reader = Files.newBufferedReader(readmePath, Charset.defaultCharset());
        String line = null;
        for (int i = 0; i < lineNumber; i++) {
            line = reader.readLine();
        }
        reader.close();
        return line;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}