0
  1. Is there a way to read chunk data by other delimiter(s) for array of chars [,|-_ etc] or split chunk not in middle of word,

  2. How to skip specific location for text file (for example skip to offset char#10020353)

Example file:

text text ; text ... (very long line 100000s chars)
text text ; text ... (very long line 100000s chars)
text text ; text ... (very long line 100000s chars)
text text ; text ... (very long line 100000s chars)

(very long file 100000s lines)

I have function

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
  stream.forEach(com.alefbt::DO_SOME_THING);
} catch (IOException e) {
  e.printStackTrace();
}
Yehuda
  • 457
  • 2
  • 6
  • 16
  • 1
    Use a `Filereader` and you can read any data size you want. – Robert Dec 21 '17 at 14:22
  • 2
    If your problem is instantiating large strings, then the only way around it while still staying within the Streams API is creating your own `Spliterator` based on a `BufferedReader(FileReader)` and implementing whatever splitting logic makes to you. If you go this way, _do not try_ to implement the `Spliterator` interface directly, start from `Spliterators.AbstractSpliterator`. – Marko Topolnik Dec 21 '17 at 14:39
  • 2
    Use a `Scanner`. Since `findAll` is not there until Java9, you can use the implementation in [this answer](https://stackoverflow.com/a/42978216/2711488) to construct a stream. Or just use a `Scanner` and a loop, if `forEach` is all you’re gonna do. Then, it’s easy to specify an arbitrary delimiter pattern. – Holger Dec 21 '17 at 16:00
  • You can split any string you want in any way you want, just use the basic `String.split()` functions. – Jean-Baptiste Yunès Dec 21 '17 at 16:30
  • 1
    @JeanBaptisteYunès: but using `String.split` requires the string to be completely loaded into memory before splitting. – Holger Dec 21 '17 at 17:01

1 Answers1

0

Print a file with 50 lines of 112000 chars within 1054ms using "," delimiter :

Files.lines(path).flatMap(line-> Stream.of(line.split(","))).forEach(System.out::println);
Fabien
  • 974
  • 9
  • 14