i ended up with a do while loop, but it looks awkward to me. is there a better way to do it?
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
try (BufferedWriter file = new BufferedWriter(new FileWriter(buff.readLine()))) {
String content;
do {
content = buff.readLine();
file.write(content);
file.newLine();
} while (!content.equals("exit"));
} catch (IOException e) { e.printStackTrace(); }
buff.close();
}
}
is there a better way to write this?
Resolved
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
29 August 2020, 06:40
I don't think there's the one solution for this sort of problems. You could use either loop and I guess the compiler will make comparable code out of it. What you could do is, to have the buff initialisation within the try-with-resources definition that way you do not need to think about closing it.
0