Guest User

Untitled

a guest
Jul 17th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2. * Уровень 1. Задача 1.
  3. * Создайте консольный «текстовый редактор» с
  4. * возможностью сохранения набранного текста в файл.
  5. */
  6.  
  7. package com.gmail.safordog;
  8.  
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.io.PrintWriter;
  13. import java.util.Scanner;
  14.  
  15. public class Main {
  16.  
  17. public static void main(String[] args) {
  18.  
  19. File text = new File("text.txt");
  20. try {
  21. if (!text.exists()) {
  22. text.createNewFile();
  23. }
  24. } catch (IOException e) {
  25. System.out.println(e);
  26. }
  27. saveText(text);
  28. }
  29.  
  30. /**
  31. * Saves the text entered in the console to the file;
  32. * @param file - created to store text file;
  33. */
  34. public static void saveText(File file) {
  35. Scanner sc = new Scanner(System.in);
  36. PrintWriter pw;
  37. System.out.println("Input text and press "
  38. + "twise ENTER to save: ");
  39. try {
  40. pw = new PrintWriter(file);
  41. for (;;) {
  42. String str = sc.nextLine();
  43. pw.println(str);
  44. if (str.equals("")) {
  45. break;
  46. }
  47. }
  48. pw.close();
  49. } catch (FileNotFoundException e) {
  50. e.printStackTrace();
  51. } finally {
  52. System.out.println("End of program.");
  53. }
  54. sc.close();
  55. }
  56.  
  57. }
Add Comment
Please, Sign In to add comment