deyanmalinov

1. Sum Lines - Streams, Files And Directories - Exercises

Apr 9th, 2020
102
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) {
  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.  
  11.         try {
  12.            BufferedReader reader = new BufferedReader(new FileReader(path));
  13.            PrintWriter writer = new PrintWriter(new FileWriter(output));
  14.             String line = reader.readLine();
  15.             while (line != null){
  16.                 int sum = 0;
  17.                 for (char chars : line.toCharArray()) {
  18.                     sum+=chars;
  19.                 }
  20.                 System.out.println(sum);
  21.                 line=reader.readLine();
  22.             }
  23.         } catch (FileNotFoundException e) {
  24.             e.printStackTrace();
  25.         } catch (IOException e) {
  26.             e.printStackTrace();
  27.         }
  28.     }
  29. }
RAW Paste Data