Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV5;
- import javax.sound.sampled.Line;
- 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 readDataWithBufferedReaderAndMapReduce(InputStream inputStream)
- {
- BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
- LineCounter result = bufferedReader.lines()
- //mapiraj od linija vo objekt
- //za sekoja linija ke se napravi nov line counter
- .map(l -> new LineCounter(l))
- //reduce prima 2 raboti->od 10 line counteri napravi 1->od poveke nesta sklopi gi vo edno nesto
- .reduce(
- new LineCounter(0, 0, 0),
- //left e momentot do rezultatot a right posle toa
- (left, right) -> left.sum(right)
- );
- //reduce e terminalna akcija
- System.out.println(result);
- }
- 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));
- System.out.println();
- readDataWithBufferedReaderAndMapReduce(new FileInputStream(file));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement