Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV5;
- import java.io.*;
- import java.util.Scanner;
- public class WordCountTest {
- public static void readDataWithScanner(InputStream inputStream) {
- int lines = 0;
- int words = 0;
- int chars = 0;
- Scanner scanner = new Scanner(inputStream);
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- ++lines;
- words += line.split("\\s++").length;
- chars += line.length();
- }
- System.out.printf("Lines : %d , Words: %d , Chars: %d", lines, words, chars);
- }
- public static void readDataWithBufferedReader(InputStream inputStream) throws IOException {
- int lines=0;
- int words=0;
- int chars=0;
- BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
- String line;
- while((line=bufferedReader.readLine())!=null)
- {
- ++lines;
- words+=line.split("\\s+").length;
- chars+=line.length();
- }
- System.out.printf("Lines : %d , Words: %d , Chars: %d", lines, words, chars);
- }
- public static void main(String[] args) throws IOException {
- File file = new File("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV5\\dat");
- readDataWithScanner(new FileInputStream(file));
- System.out.println();
- readDataWithBufferedReader(new FileInputStream(file));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement