为什么我必须等待一段时间才能在使用BufferedReader()时再次使用Ctrl + Z!

时间:2018-04-02 09:31:08

标签: java eclipse bufferedreader bufferedwriter bufferedinputstream

我有以下代码:

  import java.io.*;

public class BufferedWriteToFile {
//Read every line from br, write everything into bw
public static void br2bw(BufferedReader br, BufferedWriter bw) throws IOException {
    String z;       //Line
    while ((z = br.readLine()) != null) { //Read
                                            //Stream check
        bw.write(z);                        //writing
        bw.newLine();                       //next line
    }
    br.close();
    bw.close();
}

public static void main (String[] args) {
    try {
        File datei = new File(args[0]);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new FileWriter(datei));
        System.out.println("Type some text please");
        System.out.println("(You can stop with Ctrl+Z \n");
        br2bw(in, out); //read from in, write into out
        in = new BufferedReader(new FileReader(datei));
        out = new BufferedWriter(new OutputStreamWriter(System.out));
        System.out.println("\nIn " +args[0] + "saved Text:");
        br2bw(in, out); //read from in, write out
    }catch (IOException e) {

      }
  }

}

在Eclipse中尝试我的代码我经历过它并不总是以相同的方式工作:     1,有时它工作正常,它将键入的文本保存到文件中     2,其他一些时候如果按Ctrl + Z则根本没有任何反应     3,有时候如果我等了一段时间后再按一下蚂蚁我会再次按下它有效     4,如果Ctrl + Z有效,但我只有一行,它只保存一个空文件

请您解释一下为什么会发生以下情况,我该如何避免它们?

非常感谢您的帮助。

0 个答案:

没有答案