首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2012-01-24 
为什么这段程序只能执行一半?Java codepublic static void main(String[] args) throws IOException {Buff

为什么这段程序只能执行一半?

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


[解决办法]
对同一个文件一边读一边写,你觉得这事靠谱吗?

热点排行
Bad Request.