- 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();
-
- }
复制代码 |