代码如下:- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Test {
- public static void main(String[] args) {
- BufferedReader br = null;
- BufferedWriter bw = null;
- String b = null;
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- bw = new BufferedWriter(new FileWriter("D:/java/222.txt",true));
- while((b=br.readLine())!= null){
- if(b.equalsIgnoreCase("exit")){
- break;
- }
- System.out.println(b);
- bw.write(b);
- bw.newLine();
- }
- bw.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- bw.close();
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
复制代码 为什么这段代码要加入
if(b.equalsIgnoreCase("exit")){
break;
}
后,才能把数据写入到4.txt中,否则就是写入不进去?
|