Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import java.io.*;
  2. //import java.util.Scanner;
  3.  
  4. public class zajecia6 {
  5. public static void main(String[] args) {
  6. //Scanner scan = new Scanner(System.in);
  7. //FileReader fr=new FileReader("Z:\\zajecia\\plik.txt");
  8. FileReader fr = null;
  9. try {
  10. fr = new FileReader("Z:\\zajecia\\plik.txt");
  11. BufferedReader br = new BufferedReader(fr);
  12.  
  13. String line = br.readLine();
  14. int suma = 0;
  15. while (line != null) {
  16. int input = Integer.parseInt(line);
  17. suma = suma + input;
  18.  
  19. line = br.readLine();
  20. }
  21. br.close();
  22. System.out.println("Suma liczb to:" + suma);
  23.  
  24. try {
  25.  
  26. FileWriter fw = new FileWriter("Z:\\zajecia\\plik2.txt");
  27. BufferedWriter writer = new BufferedWriter(fw);
  28. writer.write(""+suma);
  29.  
  30. writer.close();
  31. }
  32. catch (IOException e) {
  33. System.out.println("Wystapil nieznany blad.");
  34. }
  35. }
  36. catch (FileNotFoundException e){
  37. System.out.println(e.getMessage());
  38. System.out.println("Nie ma taikiego pliku");
  39. }
  40. catch (IOException e) {
  41. System.out.println("Wystapil nieznany blad.");
  42. }
  43.  
  44.  
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement