Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Scanner;
- public class Main {
- public static final String FILE = "C:/Users/Jakub/Desktop/numeryczne/untitled1/src/com/company/test1.csv";
- public static void scanner() throws IOException{
- long start = System.nanoTime();
- Scanner scanner = new Scanner(Paths.get(FILE));
- int i = 0;
- while(scanner.hasNext()){
- scanner.nextLine();
- i++;
- }
- long stop = System.nanoTime();
- System.out.println("Scanner " + i + " Czas: " + (stop-start));
- }
- public static void bufferedreadlines() throws IOException{
- long start = System.nanoTime();
- int i = 0;
- try { BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(FILE));
- String line = null;
- while ((line = bufferedReader.readLine()) != null) {
- i++;
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- long stop = System.nanoTime();
- System.out.println("Scanner " + i + " Czas: " + (stop-start));
- }
- public static void bufferedlines() throws IOException{
- long start = System.nanoTime();
- int i = 0;
- try { BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(FILE));
- i = (int) bufferedReader.lines().count();
- } catch(Exception e) {
- e.printStackTrace();
- }
- long stop = System.nanoTime();
- System.out.println("Scanner " + i + " Czas: " + (stop-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 {
- scanner();
- bufferedreadlines();
- bufferedlines();
- inputstream();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement