Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Уровень 1. Задача 1.
- * Создайте консольный «текстовый редактор» с
- * возможностью сохранения набранного текста в файл.
- */
- package com.gmail.safordog;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- File text = new File("text.txt");
- try {
- if (!text.exists()) {
- text.createNewFile();
- }
- } catch (IOException e) {
- System.out.println(e);
- }
- saveText(text);
- }
- /**
- * Saves the text entered in the console to the file;
- * @param file - created to store text file;
- */
- public static void saveText(File file) {
- Scanner sc = new Scanner(System.in);
- PrintWriter pw;
- System.out.println("Input text and press "
- + "twise ENTER to save: ");
- try {
- pw = new PrintWriter(file);
- for (;;) {
- String str = sc.nextLine();
- pw.println(str);
- if (str.equals("")) {
- break;
- }
- }
- pw.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- System.out.println("End of program.");
- }
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment