I can create a file and save it (i.e as file.txt) and it shows the text on separated lines(including exit) but the program doesn't pass the validation.
Additionally, I also got the message from:
The program must write absolutely all of the entered lines (including "exit") to the file, each on a new line:
CodeGym security exception. You are performing a forbidden or potentially dangerous operation.
I am not sure what kind of operation it is (but it sounds like a proper badass :D and it made my day xD)
package com.codegym.task.task13.task1319;
import java.io.*;
/*
Writing to a file from the console
1. Read a file name from the console.
2. Read lines from the console until the user enters the "exit".
3. Write all the entered lines to the file, each on a new line.
Requirements:
1. The program must read the file name from the console.
2. Create and use a BufferedWriter object.
3. The program should not read anything from the file system.
4. The program must read lines from the console until the user enters "exit".
5. The program must write absolutely all of the entered lines (including "exit") to the file, each on a new line.
6. The main method should close the BufferedWriter object after using it.
*/
public class Solution {
public static void main(String[] args) throws IOException{
String userInput = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
File file = new File(reader.readLine()); //name (pathname) of the file given by the user
BufferedWriter bw = new BufferedWriter(new FileWriter(file)); // initialize the BufferWriter
if(!file.exists()){ //creates a file if not existing
file.createNewFile();
}
while (!userInput.equals("exit")) {
userInput = reader.readLine(); //read string from a user
bw.write(userInput); //writes the provided string(userInput) into the file
bw.newLine(); //writes a line separator
}
bw.close();
reader.close();
}
}