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(); } }