deyanmalinov

2. Sum Bytes - Write to File

Apr 9th, 2020
250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. public class Main {
  3.     public static void main(String[] args) throws IOException {
  4.         String path = "D:\\Coding\\BG_Softuni\\Java Advanced - January 2019" +
  5.                 "\\04.1. Java-Advanced-Files-and-Directories-Exercises\\" +
  6.                 "04. Java-Advanced-Files-and-Streams-Exercises-Resources\\input.txt";
  7.         String output = "D:\\Coding\\BG_Softuni\\Java Advanced - January 2019" +
  8.                 "\\04.1. Java-Advanced-Files-and-Directories-Exercises\\" +
  9.                 "04. Java-Advanced-Files-and-Streams-Exercises-Resources\\output.txt";
  10.         BufferedReader reader = new BufferedReader(new FileReader(path));
  11.         PrintWriter writer = new PrintWriter(new FileWriter(output));
  12.         try {
  13.             String line = reader.readLine();
  14.             int sum = 0;
  15.             while (line != null){
  16.                 for (char chars : line.toCharArray()) {
  17.                     sum+=chars;
  18.                 }
  19.                 line=reader.readLine();
  20.             }
  21.             writer.write(sum+"");
  22.             System.out.println(sum);
  23.         } catch (FileNotFoundException e) {
  24.             e.printStackTrace();
  25.         } catch (IOException e) {
  26.             e.printStackTrace();
  27.         }
  28.         writer.close();
  29.     }
  30. }
RAW Paste Data