Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StreamsFilesDirectories_Exercise;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- public class SumBytes_02 {
- public static void main(String[] args) throws IOException {
- String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\input.txt";
- long sum = 0; //сумата от всички символи във файла
- //начин 1
- /*BufferedReader br = new BufferedReader(new FileReader(path));
- String line = br.readLine();
- //спираме: line == null
- while (line != null) {
- for (int index = 0; index < line.length(); index++) {
- char currentSymbol = line.charAt(index);
- sum += currentSymbol;
- }
- line = br.readLine();
- }
- System.out.println(sum);*/
- //начин 2
- byte [] allBytes = Files.readAllBytes(Path.of(path));
- for (byte ascii : allBytes) {
- if (ascii != 10 && ascii != 13) {
- sum += ascii;
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement