class hello {
public static void main(String[] args) throws IOException {
String s;
Scanner sc = new Scanner(System.in);
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Lee\\Desktop\\text.txt"));
System.out.println("请输入内容:");
while (true) {
s = sc.nextLine();
if ("exit".equals(s)) {
System.out.println("Exit!");
break;
}
output.write(s.getBytes());
output.write("\r\n".getBytes());
}
output.close();
}
在控制台中录入数据:
运行结果: