当前位置: 代码迷 >> J2SE >> 为什么这段程序只能执行一半?解决思路
  详细解决方案

为什么这段程序只能执行一半?解决思路

热度:93   发布时间:2016-04-24 12:18:30.0
为什么这段程序只能执行一半?
Java code
    public static void main(String[] args) throws IOException {        BufferedWriter bw = new BufferedWriter(new FileWriter("c:/s1.txt"));        BufferedReader br = new BufferedReader(new FileReader("c:/s1.txt"));        Scanner input = new Scanner(System.in);        while (true) {            System.out                    .println("Please input a sentence(input \"end\" to exit):");            String str = input.nextLine();            if (str.toUpperCase().equals("END")) {                break;            }            bw.write(str.toUpperCase());            bw.newLine();        }//到这里时,运行正确,文件写入内容也符合预期,但是下面的读取操作却没有返回任何结果,为什么?        String str = "";        while ((str = br.readLine()) != null) {            System.out.println(str);        }//或者使用如下代码                  //while(br.ready()){        //    System.out.println(br.readLine());        //}//这样写正确吗?        bw.close();        br.close();    }


------解决方案--------------------
对同一个文件一边读一边写,你觉得这事靠谱吗?
  相关解决方案