Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9. public static final String FILE = "C:/Users/Jakub/Desktop/numeryczne/untitled1/src/com/company/test1.csv";
  10.  
  11. public static void scanner() throws IOException{
  12. long start = System.nanoTime();
  13. Scanner scanner = new Scanner(Paths.get(FILE));
  14. int i = 0;
  15. while(scanner.hasNext()){
  16. scanner.nextLine();
  17. i++;
  18. }
  19. long stop = System.nanoTime();
  20. System.out.println("Scanner " + i + " Czas: " + (stop-start));
  21. }
  22.  
  23. public static void bufferedreadlines() throws IOException{
  24. long start = System.nanoTime();
  25. int i = 0;
  26. try { BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(FILE));
  27. String line = null;
  28. while ((line = bufferedReader.readLine()) != null) {
  29. i++;
  30. }
  31. } catch(Exception e) {
  32. e.printStackTrace();
  33. }
  34. long stop = System.nanoTime();
  35. System.out.println("Scanner " + i + " Czas: " + (stop-start));
  36. }
  37.  
  38. public static void bufferedlines() throws IOException{
  39. long start = System.nanoTime();
  40. int i = 0;
  41. try { BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(FILE));
  42. i = (int) bufferedReader.lines().count();
  43. } catch(Exception e) {
  44. e.printStackTrace();
  45. }
  46. long stop = System.nanoTime();
  47. System.out.println("Scanner " + i + " Czas: " + (stop-start));
  48. }
  49.  
  50. private static void inputStream() throws IOException {
  51. long start = System.nanoTime();
  52. InputStream is = new BufferedInputStream(new FileInputStream(FILE));
  53. int counter = 0;
  54. byte[] c = new byte[1024];
  55. int readChars = 0;
  56. while ( (readChars = is.read(c)) != -1 ) {
  57. for ( int i = 0; i < readChars; i++ ) {
  58. if ( c[i] == '\n' ) {
  59. counter++;
  60. }
  61. }
  62. }
  63. long end = System.nanoTime();
  64. System.out.println("INPUT READER: " + counter + " czas: " + (end - start));
  65. }
  66.  
  67.  
  68.  
  69.  
  70. public static void main(String[] args) throws IOException {
  71. scanner();
  72. bufferedreadlines();
  73. bufferedlines();
  74. inputstream();
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement