Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package pl.pl;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.Scanner;
  7. import java.util.stream.Stream;
  8.  
  9. class Main {
  10.  
  11. private static final String FILE = "/home/LABPK/jsildatk/Pobrane/test";
  12.  
  13. private static void bufferedReadLine() throws IOException {
  14. int counter = 0;
  15. long start = System.nanoTime();
  16. try ( BufferedReader br = Files.newBufferedReader(Paths.get(FILE)) ) {
  17. String line;
  18. while ( (line = br.readLine()) != null ) {
  19. counter++;
  20. }
  21. }
  22. long end = System.nanoTime();
  23. System.out.println("BUFFERED READER READ LINE: " + counter + " czas: " + (end - start));
  24. }
  25.  
  26. private static void bufferedLines() throws IOException {
  27. long start = System.nanoTime();
  28. int lines = 0;
  29. try ( BufferedReader br = Files.newBufferedReader(Paths.get(FILE)) ) {
  30. lines = (int) br.lines().count();
  31. }
  32. long end = System.nanoTime();
  33. System.out.println("BUFFERED READER LINES: " + lines + " czas: " + (end - start));
  34. }
  35.  
  36. private static void scannerNextLine() throws IOException {
  37. long start = System.nanoTime();
  38. Scanner scanner = new Scanner(Paths.get(FILE));
  39. int counter = 0;
  40. while ( scanner.hasNext() ) {
  41. scanner.nextLine();
  42. counter++;
  43. }
  44. long end = System.nanoTime();
  45. System.out.println("SCANNER: " + counter + " czas: " + (end - start));
  46. }
  47.  
  48. private static void inputStream() throws IOException {
  49. long start = System.nanoTime();
  50. InputStream is = new BufferedInputStream(new FileInputStream(FILE));
  51. int counter = 0;
  52. byte[] c = new byte[1024];
  53. int readChars = 0;
  54. while ( (readChars = is.read(c)) != -1 ) {
  55. for ( int i = 0; i < readChars; i++ ) {
  56. if ( c[i] == '\n' ) {
  57. counter++;
  58. }
  59. }
  60. }
  61. long end = System.nanoTime();
  62. System.out.println("INPUT READER: " + counter + " czas: " + (end - start));
  63. }
  64.  
  65. public static void main(String[] args) throws IOException {
  66. inputStream();
  67. bufferedReadLine();
  68. bufferedLines();
  69. scannerNextLine();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement