package com.codegym.task.task13.task1319;
import java.io.*;
/*
Writing to a file from the console
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
File file = new File("c:/progs/files/CodeGym/" + fileName);
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
while (true) {
String s = reader.readLine();
if (!s.equals("exit")) {
bw.write(s + "\n");
// System.out.println(s + " -in if");
}
else {
bw.write(s);
break;
}
}
reader.close();
fw.close();
reader.close();
}
}
Everything works, it takes file name from the console (though you have to include the file type extension and not sure if the is the right way or not), it creates the file in the proper location, it takes lines from the console until "exit" is input and then ends like it supposed to, but when I go to look at the file it is completely blank...cant figure out why the program isn't writing to file...
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
11 November 2019, 15:58
The user enters the full filepath, which means that anything you add to it fails. Fix line 15.
Also you are closing reader twice. I think you meant to close bw on one of them.
0