Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.pl;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Scanner;
- import java.util.stream.Stream;
- class Main {
- private static final String FILE = "/home/LABPK/jsildatk/Pobrane/test";
- private static void bufferedReadLine() throws IOException {
- int counter = 0;
- long start = System.nanoTime();
- try ( BufferedReader br = Files.newBufferedReader(Paths.get(FILE)) ) {
- String line;
- while ( (line = br.readLine()) != null ) {
- counter++;
- }
- }
- long end = System.nanoTime();
- System.out.println("BUFFERED READER READ LINE: " + counter + " czas: " + (end - start));
- }
- private static void bufferedLines() throws IOException {
- long start = System.nanoTime();
- int lines = 0;
- try ( BufferedReader br = Files.newBufferedReader(Paths.get(FILE)) ) {
- lines = (int) br.lines().count();
- }
- long end = System.nanoTime();
- System.out.println("BUFFERED READER LINES: " + lines + " czas: " + (end - start));
- }
- private static void scannerNextLine() throws IOException {
- long start = System.nanoTime();
- Scanner scanner = new Scanner(Paths.get(FILE));
- int counter = 0;
- while ( scanner.hasNext() ) {
- scanner.nextLine();
- counter++;
- }
- long end = System.nanoTime();
- System.out.println("SCANNER: " + counter + " czas: " + (end - start));
- }
- private static void inputStream() throws IOException {
- long start = System.nanoTime();
- InputStream is = new BufferedInputStream(new FileInputStream(FILE));
- int counter = 0;
- byte[] c = new byte[1024];
- int readChars = 0;
- while ( (readChars = is.read(c)) != -1 ) {
- for ( int i = 0; i < readChars; i++ ) {
- if ( c[i] == '\n' ) {
- counter++;
- }
- }
- }
- long end = System.nanoTime();
- System.out.println("INPUT READER: " + counter + " czas: " + (end - start));
- }
- public static void main(String[] args) throws IOException {
- inputStream();
- bufferedReadLine();
- bufferedLines();
- scannerNextLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement