Java Code Examples for java.io.BufferedReader#lines()

The following examples show how to use java.io.BufferedReader#lines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lines.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 2
Source File: Lines.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 3
Source File: PathLineIterator.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns an iterator so you can iterate over the lines in the text file like so:
 * for (String line: new PathLineIterator(path)) {
 *   // do something with the line
 * }
 *
 * It's also closeable so you can close it when done, or use it in a try-with-resources
 * to close it automatically.
 *
 * If the file name ends in ".gz", this will decompress it automatically.
 *
 * Consider also using XReadLines if you need trimming or skipping comments.
 *
 * @param path path to a text file.
 */
public PathLineIterator(final Path path) {
    try {
        InputStream is = Files.newInputStream(Utils.nonNull(path, "path shouldn't be null"));
        if (IOUtil.hasBlockCompressedExtension(path)) {
            is = IOUtils.makeZippedInputStream(is);
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        lines = br.lines();
    }
    catch (final CharacterCodingException ex ) {
        throw new UserException("Error detected in file character encoding.  Possible inconsistent character encodings within the file: " + path.toUri().toString(), ex);
    }
    catch (final IOException x) {
        throw new UserException("Error reading " + path.toUri().toString(), x);
    }
}
 
Example 4
Source File: Lines.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 5
Source File: LoggingServiceImpl.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Override
public Page<String> read(ExecutedCmd cmd, Pageable pageable) {
    BufferedReader reader = getReader(cmd.getId());

    if (Objects.isNull(reader)) {
        return LogNotFound;
    }

    try (Stream<String> lines = reader.lines()) {
        int i = pageable.getPageNumber() * pageable.getPageSize();

        List<String> logs = lines.skip(i)
            .limit(pageable.getPageSize())
            .collect(Collectors.toList());

        return new PageImpl<>(logs, pageable, cmd.getLogSize());
    } finally {
        try {
            reader.reset();
        } catch (IOException e) {
            // reset will be failed if all lines been read
            logReaderCache.invalidate(cmd.getId());
        }
    }
}
 
Example 6
Source File: Lines.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 7
Source File: Lines.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 8
Source File: Lines.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 9
Source File: LoggingServiceImpl.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
@Override
public Page<String> read(Step cmd, Pageable pageable) {
    BufferedReader reader = getReader(cmd.getId());

    if (Objects.isNull(reader)) {
        return LogNotFound;
    }

    try (Stream<String> lines = reader.lines()) {
        int i = pageable.getPageNumber() * pageable.getPageSize();

        List<String> logs = lines.skip(i)
                .limit(pageable.getPageSize())
                .collect(Collectors.toList());

        return new PageImpl<>(logs, pageable, cmd.getLogSize());
    } finally {
        try {
            reader.reset();
        } catch (IOException e) {
            // reset will be failed if all lines been read
            logReaderCache.invalidate(cmd.getId());
        }
    }
}
 
Example 10
Source File: Lines.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testIterator() throws IOException {
    MockLineReader r = new MockLineReader(6);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    assertEquals(r.getLineNumber(), 1, "Read one line");
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();
    // Ensure iterate with only next works
    for (int i = 0; i < 5; i++) {
        String str = it.next();
        assertEquals(str, "Line " + (i + 2), "Addtional five lines");
    }
    // NoSuchElementException
    try {
        it.next();
        fail("Should have run out of lines.");
    } catch (NoSuchElementException nsse) {}
}
 
Example 11
Source File: DefaultAppRegistryService.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private Stream<String> resourceAsLines(Resource resource) {
	try {
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
		return bufferedReader.lines();
	}
	catch (Exception e) {
		throw new RuntimeException("Error reading from " + resource.getDescription(), e);
	}
}
 
Example 12
Source File: Lines.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 13
Source File: Lines.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 14
Source File: Lines.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 15
Source File: Lines.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 16
Source File: Lines.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 17
Source File: Lines.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 18
Source File: Writer.java    From neo4j-mazerunner with Apache License 2.0 5 votes vote down vote up
public static void asyncPartitionedUpdate(BufferedReader bufferedReader, GraphDatabaseService graphDb, ProcessorMessage processorMessage) throws IOException {

        Integer reportBlockSize = 10000;

        Stream<String> iterator = bufferedReader.lines();

        List<Spliterator<String>> spliteratorList = new ArrayList<>();
        boolean hasSpliterator = true;
        Spliterator<String> nodeSpliterator = iterator.spliterator();

        while (hasSpliterator) {
            Spliterator<String> localSpliterator = nodeSpliterator.trySplit();
            hasSpliterator = localSpliterator != null;
            if (hasSpliterator)
                spliteratorList.add(localSpliterator);
        }

        counter = 0;
        if (spliteratorList.size() > 4) {
            // Fork join
            ParallelBatchTransaction parallelBatchTransaction =
                    new ParallelBatchTransaction(spliteratorList.toArray(new Spliterator[spliteratorList.size()]),
                            0, spliteratorList.size(), graphDb, reportBlockSize, spliteratorList.size(), processorMessage);

            ForkJoinPool pool = new ForkJoinPool();
            pool.invoke(parallelBatchTransaction);
        } else {
            // Sequential
            Transaction tx = graphDb.beginTx();
            Node partitionNode = graphDb.getNodeById(processorMessage.getPartitionDescription().getPartitionId());
            spliteratorList.forEach(sl -> sl.forEachRemaining(n -> updatePartitionBlockForRow(n, graphDb, reportBlockSize, processorMessage, partitionNode)));
            tx.success();
            tx.close();
        }

        System.out.println("Job completed");
    }
 
Example 19
Source File: Lines.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}
 
Example 20
Source File: Lines.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void testInterlacedRead() throws IOException {
    MockLineReader r = new MockLineReader(10);
    BufferedReader br = new BufferedReader(r);
    char[] buf = new char[5];
    Stream<String> s = br.lines();
    Iterator<String> it = s.iterator();

    br.read(buf);
    assertEquals(new String(buf), "Line ");
    assertEquals(it.next(), "1");
    try {
        s.iterator().next();
        fail("Should failed on second attempt to get iterator from s");
    } catch (IllegalStateException ise) {}
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    // Get stream again should continue from where left
    // Only read remaining of the line
    br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "Li");
    br.read(buf, 0, 2);
    assertEquals(new String(buf, 0, 2), "ne");
    assertEquals(it.next(), " 3");
    // Line 4
    br.readLine();
    // interator pick
    assertEquals(it.next(), "Line 5");
    // Another stream instantiated by lines()
    AtomicInteger line_no = new AtomicInteger(6);
    br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
    // Read after EOL
    assertFalse(it.hasNext());
}