Advertisement
jaVer404

level13.lesson11.home04_last line before exit

May 21st, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package com.javarush.test.level13.lesson11.home04;
  2.  
  3. /* Запись в файл
  4. 1. Прочесть с консоли имя файла.
  5. 2. Считывать строки с консоли, пока пользователь не введет строку "exit".
  6. 3. Вывести все строки в файл, каждую строчку с новой стороки.
  7. */
  8.  
  9. import java.io.*;
  10.  
  11.  
  12. public class Solution
  13. {
  14.     public static void main(String[] args) throws IOException
  15.     {
  16.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  17.         String fileName = reader.readLine();
  18.         String input;
  19.         while (true) {
  20.             input = reader.readLine();
  21.             if (input.equals("exit")) {
  22.               reader.close();
  23.                 break;
  24.             }
  25.             writeToFile(fileName, input);
  26.         }
  27.  
  28.     }
  29.     public static void writeToFile (String fileToWrite,String textToWrite) throws IOException{
  30.         String outPutPath = fileToWrite;
  31.         FileWriter fileWriter = new FileWriter(outPutPath);
  32.         fileWriter.write(textToWrite + "\n");
  33.         fileWriter.close();
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement