Advertisement
desislava_topuzakova

2. Sum Bytes

Jan 22nd, 2022
500
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package StreamsFilesDirectories_Exercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9.  
  10. public class SumBytes_02 {
  11.     public static void main(String[] args) throws IOException {
  12.         String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\input.txt";
  13.         long sum = 0; //сумата от всички символи във файла
  14.         //начин 1
  15.         /*BufferedReader br = new BufferedReader(new FileReader(path));
  16.         String line = br.readLine();
  17.         //спираме: line == null
  18.         while (line != null) {
  19.             for (int index = 0; index < line.length(); index++) {
  20.                 char currentSymbol = line.charAt(index);
  21.                 sum += currentSymbol;
  22.             }
  23.             line = br.readLine();
  24.         }
  25.         System.out.println(sum);*/
  26.  
  27.         //начин 2
  28.         byte [] allBytes = Files.readAllBytes(Path.of(path));
  29.         for (byte ascii : allBytes) {
  30.             if (ascii != 10 && ascii != 13) {
  31.                 sum += ascii;
  32.             }
  33.         }
  34.         System.out.println(sum);
  35.     }
  36. }
  37.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement