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:br.bireme.ngrams.NGrams.java

public static void index(final NGIndex index, final NGSchema schema, final String inFile,
        final String inFileEncoding) throws IOException, ParseException {
    if (index == null) {
        throw new NullPointerException("index");
    }//from  www .  j a va2s.co  m
    if (schema == null) {
        throw new NullPointerException("schema");
    }
    if (inFile == null) {
        throw new NullPointerException("inFile");
    }
    if (inFileEncoding == null) {
        throw new NullPointerException("inFileEncoding");
    }

    final Charset charset = Charset.forName(inFileEncoding);
    final IndexWriter writer = index.getIndexWriter(false);
    int cur = 0;

    try (BufferedReader reader = Files.newBufferedReader(new File(inFile).toPath(), charset)) {
        writer.deleteAll();

        while (true) {
            final String line;
            try {
                line = reader.readLine();
            } catch (MalformedInputException mie) {
                System.err.println("Line with another encoding. Line number:" + (++cur));
                continue;
            }
            if (line == null) {
                break;
            }
            final boolean ret = indexDocument(index, writer, schema, line, false);
            if (ret && (++cur % 100000 == 0)) {
                System.out.println(">>> " + cur);
            }
        }
        writer.forceMerge(1); // optimize index
        writer.close();
    }
}

From source file:org.apache.tika.cli.TikaCLIBatchIT.java

@Test
public void testJsonRecursiveBatchIntegration() throws Exception {
    String[] params = { "-i", testInputDirForCommandLine, "-o", tempOutputDirForCommandLine, "-numConsumers",
            "10", "-J", //recursive Json
            "-t" //plain text in content
    };//from   w ww  . ja  v  a2  s  .  c  o  m
    runFramework(params);

    Path jsonFile = tempOutputDir.resolve("test_recursive_embedded.docx.json");
    try (Reader reader = Files.newBufferedReader(jsonFile, UTF_8)) {
        List<Metadata> metadataList = JsonMetadataList.fromJson(reader);
        assertEquals(12, metadataList.size());
        assertTrue(metadataList.get(6).get(RecursiveParserWrapper.TIKA_CONTENT).contains("human events"));
    }
}

From source file:filtercdnafile.CreateCdnaProteinFiles.java

/**
 * reads the transcript file and writes the lines with the same id as in the transcriptList in to
 * a new file./*from w w  w . j a v a  2s  . c om*/
 * @throws ParseException
 * @throws IOException 
 */
private void readCDNAFasta() throws ParseException, IOException {
    Pattern re = Pattern.compile("(?<=>)ENSRNOT\\d+");
    FileWriter writer = new FileWriter();
    Charset charset = Charset.forName("US-ASCII");
    try (BufferedReader reader = Files.newBufferedReader(transcriptFile, charset)) {
        String line;
        writer.OpenFile(newTranscriptFile);
        while ((line = reader.readLine()) != null) {
            if (line.contains(">")) {
                if (transcriptID.isEmpty() == false & transcriptSequence.isEmpty() == false) {
                    Matcher m = re.matcher(transcriptID);
                    m.find();
                    //if the transcript id is in the transcript list
                    //the line is added to a new file.
                    if (transcriptList.contains(m.group())) {
                        writer.writeLine(transcriptID + "\t" + transcriptSequence);
                    }
                }
                transcriptID = line;
                transcriptSequence = "";

            } else {
                transcriptSequence += line.trim();
            }
        }
        // information of the last line is checked.
        if (transcriptList.contains(transcriptID)) {
            writer.writeLine(transcriptID + "\t" + transcriptSequence);
        }
        writer.CloseFile();
    } catch (IOException x) {
        System.err.format("IOException: %s%n", x);
    }
}

From source file:com.liferay.sync.engine.SyncSystemTest.java

@Test
public void run() throws Exception {
    SyncEngine.start();/*from www. jav a  2 s.co m*/

    _rootFilePathName = FileUtil.getFilePathName(System.getProperty("user.home"), "liferay-sync-test");

    _syncAccount = SyncAccountService.addSyncAccount(FileUtil.getFilePathName(_rootFilePathName, "test"),
            "test@liferay.com", Integer.MAX_VALUE, "test", "test", 5, null, false, "http://localhost:8080");

    SyncAccountService.update(_syncAccount);

    long guestGroupId = SyncSystemTestUtil.getGuestGroupId(_syncAccount.getSyncAccountId());

    _syncSiteIds.put("Guest", guestGroupId);

    BufferedReader bufferedReader = Files.newBufferedReader(_testFilePath, Charset.defaultCharset());

    ObjectMapper objectMapper = new ObjectMapper();

    JsonNode rootJsonNode = objectMapper.readTree(bufferedReader);

    executeSteps(_testFilePath, rootJsonNode);

    Path testFileNameFilePath = _testFilePath.getFileName();

    String testFileName = testFileNameFilePath.toString();

    _logger.info("Test {} passed.", FilenameUtils.removeExtension(testFileName));
}

From source file:de.tu_dortmund.ub.data.util.TPUUtil.java

private static void checkResultForError(final String fileName) throws IOException, TPUException {

    final Path filePath = Paths.get(fileName);
    final char[] buffer = new char[MAX_BUFFER_LENGTH];
    BufferedReader bufferedReader = Files.newBufferedReader(filePath, Charsets.UTF_8);
    final int readCharacters = bufferedReader.read(buffer, 0, MAX_BUFFER_LENGTH);

    if (readCharacters <= -1) {

        LOG.debug("couldn't check file for errors; no file content in file '{}'", fileName);

        bufferedReader.close();//  ww w  .  j  a va 2 s.  c o  m

        return;
    }

    final String bufferString = String.valueOf(buffer);

    if (bufferString.startsWith(ERROR_MESSAGE_START)) {

        bufferedReader.close();

        throw new TPUException(bufferString);
    }
}

From source file:com.talkdesk.geo.GeoCodeRepositoryBuilder.java

/**
 * Format of the file loading data from/*  www .  j av a  2 s .c  om*/
 * geonameid         : integer id of record in geonames database
 * name              : name of geographical point (utf8) varchar(200)
 * asciiname         : name of geographical point in plain ascii characters, varchar(200)
 * alternatenames    : alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000)
 * latitude          : latitude in decimal degrees (wgs84)
 * longitude         : longitude in decimal degrees (wgs84)
 * feature class     : see http://www.geonames.org/export/codes.html, char(1)
 * feature code      : see http://www.geonames.org/export/codes.html, varchar(10)
 * country code      : ISO-3166 2-letter country code, 2 characters
 * cc2               : alternate country codes, comma separated, ISO-3166 2-letter country code, 60 characters
 * admin1 code       : fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20)
 * admin2 code       : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
 * admin3 code       : code for third level administrative division, varchar(20)
 * admin4 code       : code for fourth level administrative division, varchar(20)
 * population        : bigint (8 byte int)
 * elevation         : in meters, integer
 * dem               : digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat.
 * timezone          : the timezone id (see file timeZone.txt) varchar(40)
 * modification date : date of last modification in yyyy-MM-dd format
 *
 * @throws IOException
 */
public void populateGeoData() throws GeoResolverException {

    try {
        if (connection == null)
            connection = connectToDatabase();
        if (!new File(geocodeDataLocation).exists()) {
            log.error("No Data file found for geoData. please add to data/geodata.tsv ");
            return;
        }

        Path file = FileSystems.getDefault().getPath(geocodeDataLocation);
        Charset charset = Charset.forName("UTF-8");
        BufferedReader inputStream = Files.newBufferedReader(file, charset);
        String buffer;
        PreparedStatement preparedStatement;
        preparedStatement = connection
                .prepareStatement("INSERT INTO geocodes (ID , CITY_NAME, LATITUDE, LONGITUDE, COUNTRY_CODE)"
                        + " VALUES (?,?,?,?,?)");
        while ((buffer = inputStream.readLine()) != null) {
            String[] values = buffer.split("\t");

            preparedStatement.setInt(1, Integer.parseInt(values[0].trim()));
            preparedStatement.setString(2, values[1].trim());
            preparedStatement.setFloat(3, Float.parseFloat(values[4].trim()));
            preparedStatement.setFloat(4, Float.parseFloat(values[5].trim()));
            preparedStatement.setString(5, values[8].trim());

            preparedStatement.execute();

        }
    } catch (SQLException e) {
        throw new GeoResolverException("Error while executing SQL query", e);
    } catch (IOException e) {
        throw new GeoResolverException("Error while accessing input file", e);
    }
    log.info("Finished populating Database.");
    //should close all the connections for memory leaks.
}

From source file:org.jboss.as.test.integration.logging.profiles.NonExistingProfileTestCase.java

@Test
public void warningMessageTest() throws IOException {
    boolean warningFound = false;
    try (final BufferedReader reader = Files.newBufferedReader(loggingTestLog, StandardCharsets.UTF_8)) {
        String line;//from  w  ww.  j  a  v a 2s  .  c  o  m
        while ((line = reader.readLine()) != null) {
            // Look for profile id
            if (line.contains("non-existing-profile")) {
                warningFound = true;
                break;
            }
        }
    }
    Assert.assertTrue(warningFound);
}

From source file:org.omegat.util.FileUtil.java

/**
 * Copy file and create output directory if need. EOL will be converted into target-specific or into
 * platform-specific if target doesn't exist.
 *//*from   w  w w.ja v a2  s .  c om*/
public static void copyFileWithEolConversion(File inFile, File outFile, Charset charset) throws IOException {
    File dir = outFile.getParentFile();
    if (!dir.exists()) {
        dir.mkdirs();
    }
    String eol;
    if (outFile.exists()) {
        // file exist - read EOL from file
        eol = getEOL(outFile, charset);
    } else {
        // file not exist - use system-dependent
        eol = System.lineSeparator();
    }
    try (BufferedReader in = Files.newBufferedReader(inFile.toPath(), charset)) {
        try (BufferedWriter out = Files.newBufferedWriter(outFile.toPath(), charset)) {
            String s;
            while ((s = in.readLine()) != null) {
                // copy using known EOL
                out.write(s);
                out.write(eol);
            }
        }
    }
}

From source file:org.darkware.wpman.security.ChecksumDatabase.java

/**
 * Load the database from the attached file. The file path is set in the constructor.
 *//*w w  w .  j ava2 s .  co  m*/
public void loadDatabase() {
    this.lock.writeLock().lock();
    try {
        this.hashes.clear();

        ChecksumDatabase.log.info("Reading integrity database: {}", this.dbFile);
        try (BufferedReader db = Files.newBufferedReader(this.dbFile, StandardCharsets.UTF_8)) {
            String line;
            while ((line = db.readLine()) != null) {
                int sep = line.lastIndexOf(':');
                if (sep == -1)
                    continue;

                String path = line.substring(0, sep);
                String checksum = line.substring(sep + 1);

                this.hashes.put(Paths.get(path), checksum);
            }
            ChecksumDatabase.log.info("Read {} entries into the integrity database", this.hashes.size());
        } catch (IOException e) {
            ChecksumDatabase.log.error("Error while loading integrity database: {}", e.getLocalizedMessage(),
                    e);
        }
    } finally {
        this.lock.writeLock().unlock();
    }
}

From source file:org.ng200.openolympus.FileAccess.java

public static BufferedReader newBufferedReader(final Path path) throws IOException {
    return Files.newBufferedReader(path, Charset.forName("UTF-8"));
}