日期:2014-05-20  浏览次数:20721 次

麻烦大家帮我看看这个程序,感谢了!
麻烦了哦!
就是在读取后,再写入myjava1.txt的时候,在myjava1.txt中没有数据,谢谢了!


import   java.io.FileWriter;
import   java.io.BufferedWriter;
import   java.io.FileReader;
import   java.io.BufferedReader;
import   java.io.IOException;
import   java.io.File;
public   class   CopyTest   {
                  CopyTest(){

                  }
void   check(){
/*if(args.length==0){
System.out.println( "请输入参数! ");
System.exit(1);
}*/
              File   file=new   File( "e:\\myjava\\myjava.txt ");
              if(file.exists()==true){
            System.out.println( "文件存在 ");
                    System.out.println(file.length());
              }
              else{
            System.out.println( "文件不存在! ");
            System.exit(1);
              }

              if(file.isFile()==false){
              System.exit(1);
              }

//

}
void   reading_and_copy(){
//将文件里的数据读出
try{

FileReader   fr=new   FileReader( "e:\\myjava\\myjava.txt ");
BufferedReader   br=new   BufferedReader(fr);
System.out.println( "内容为: ");
String   line=(String)br.readLine();

      // 将读出来的数据写入到其他文件中;
FileWriter   fw=new   FileWriter( "e:\\myjava\\myjava1.txt ");
BufferedWriter   bw=new   BufferedWriter(fw);

while(line!=null){
System.out.println(line);
bw.write(line);
line=br.readLine();
}
br.close();
fr.close();
fw.close();
br.close();

      }catch(IOException   e){
      System.out.println(e.toString());
      }
}
public   static   void   main(String[]   args)   {
                      CopyTest   filetest=new   CopyTest();
                      filetest.check();
                      filetest.reading_and_copy();
}

}


------解决方案--------------------
在 fw.close(); 前面
加上 bw.flush();
------解决方案--------------------
我咋看着像是要出异常呢?

br.close();
fr.close();
fw.close();
br.close();


------解决方案--------------------
CpRcF(尘)

正确
------解决方案--------------------
BufferedWriter 用的时候需要清空缓冲区 才能把你要的写进去
在循环中加上bw.flush();
while(line!=null){
System.out.println(line);
bw.write(line);
bw.flush();
line=br.readLine();