- package com.heima.test;
- import java.io.BufferedOutputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Scanner;
- public class Test3 {
- /**
- * @param args
- *
- *
- * 分析:
- * 1,创建键盘录入对象
- * 2,创建输出流对象,关联text.txt文件
- * 3,定义无限循环
- * 4,遇到quit退出循环
- * 5,如果不quit,就将内容写出
- * 6,关闭流
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入数据:");
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("text.txt"));
- while (true) {
- String line = sc.nextLine();
- if ("quit".equals(line)) {
- break;
- }
- bos.write(line.getBytes());
- bos.write("\r\n".getBytes());
- }
- bos.close();
-
- }
- }
复制代码 |
|