|
| 1 | +package com.baeldung.files; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.LineNumberReader; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.channels.FileChannel; |
| 10 | +import java.nio.channels.FileChannel.MapMode; |
| 11 | +import java.nio.charset.Charset; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.nio.file.StandardOpenOption; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Scanner; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +import org.apache.commons.io.FileUtils; |
| 20 | +import org.apache.commons.io.LineIterator; |
| 21 | + |
| 22 | +public class NumberOfLineFinder { |
| 23 | + |
| 24 | + public static int getTotalNumberOfLinesUsingBufferedReader(String fileName) { |
| 25 | + int lines = 0; |
| 26 | + try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { |
| 27 | + while (reader.readLine() != null) { |
| 28 | + lines++; |
| 29 | + } |
| 30 | + } catch (IOException ioe) { |
| 31 | + ioe.printStackTrace(); |
| 32 | + } |
| 33 | + return lines; |
| 34 | + } |
| 35 | + |
| 36 | + public static int getTotalNumberOfLinesUsingLineNumberReader(String fileName) { |
| 37 | + int lines = 0; |
| 38 | + try (LineNumberReader reader = new LineNumberReader(new FileReader(fileName))) { |
| 39 | + reader.skip(Integer.MAX_VALUE); |
| 40 | + lines = reader.getLineNumber() + 1; |
| 41 | + } catch (IOException ioe) { |
| 42 | + ioe.printStackTrace(); |
| 43 | + } |
| 44 | + return lines; |
| 45 | + } |
| 46 | + |
| 47 | + public static int getTotalNumberOfLinesUsingScanner(String fileName) { |
| 48 | + int lines = 0; |
| 49 | + try (Scanner scanner = new Scanner(new FileReader(fileName))) { |
| 50 | + while (scanner.hasNextLine()) { |
| 51 | + scanner.nextLine(); |
| 52 | + lines++; |
| 53 | + } |
| 54 | + } catch (IOException ioe) { |
| 55 | + ioe.printStackTrace(); |
| 56 | + } |
| 57 | + return lines; |
| 58 | + } |
| 59 | + |
| 60 | + public static int getTotalNumberOfLinesUsingNIOFiles(String fileName) { |
| 61 | + int lines = 0; |
| 62 | + try (Stream<String> fileStream = Files.lines(Paths.get(fileName))) { |
| 63 | + lines = (int) fileStream.count(); |
| 64 | + } catch (IOException ioe) { |
| 65 | + ioe.printStackTrace(); |
| 66 | + } |
| 67 | + return lines; |
| 68 | + } |
| 69 | + |
| 70 | + public static int getTotalNumberOfLinesUsingNIOFileChannel(String fileName) { |
| 71 | + int lines = 1; |
| 72 | + try (FileChannel channel = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ)) { |
| 73 | + ByteBuffer byteBuffer = channel.map(MapMode.READ_ONLY, 0, channel.size()); |
| 74 | + while (byteBuffer.hasRemaining()) { |
| 75 | + byte currentChar = byteBuffer.get(); |
| 76 | + if (currentChar == '\n') { |
| 77 | + lines++; |
| 78 | + } |
| 79 | + } |
| 80 | + } catch (IOException ioe) { |
| 81 | + ioe.printStackTrace(); |
| 82 | + } |
| 83 | + return lines; |
| 84 | + } |
| 85 | + |
| 86 | + public static int getTotalNumberOfLinesUsingApacheCommonsIO(String fileName) { |
| 87 | + int lines = 0; |
| 88 | + try { |
| 89 | + LineIterator lineIterator = FileUtils.lineIterator(new File(fileName)); |
| 90 | + while (lineIterator.hasNext()) { |
| 91 | + lineIterator.nextLine(); |
| 92 | + lines++; |
| 93 | + } |
| 94 | + } catch (IOException ioe) { |
| 95 | + ioe.printStackTrace(); |
| 96 | + } |
| 97 | + return lines; |
| 98 | + } |
| 99 | + |
| 100 | + public static int getTotalNumberOfLinesUsingGoogleGuava(String fileName) { |
| 101 | + int lines = 0; |
| 102 | + try { |
| 103 | + List<String> lineItems = com.google.common.io.Files.readLines(Paths.get(fileName) |
| 104 | + .toFile(), Charset.defaultCharset()); |
| 105 | + lines = lineItems.size(); |
| 106 | + } catch (IOException ioe) { |
| 107 | + ioe.printStackTrace(); |
| 108 | + } |
| 109 | + return lines; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments