public class CopyTextTest_2 {

  private static final int BUFFER_SIZE = 1024;
 public static void main(String[] args) {   FileReader fr = null;
  FileWriter fw = null;
  try {
   //创建字节输入流,选择要读取数据的文件
   fr = new FileReader("IO流_2.txt");
   //创建字节输出流,选择要写入数据的文件
   fw = new FileWriter("copytest_2.txt");
   
   //创建一个临时容器,用于缓存读取到的字符。
   char[] buf = new char[BUFFER_SIZE];//这就是缓冲区。
   
   //定义一个变量记录读取到的字符数,(其实就是往数组里装的字符个数 )
   int len = 0;
   //循环从输入流中取出的数据
   while((len=fr.read(buf))!=-1){
  //每读取一次,即写入文件输入流,读多少写多少
    fw.write(buf, 0, len);
   }
   
  } catch (Exception e) {
//   System.out.println("读写失败");
   throw new RuntimeException("读写失败");
  }finally{
   if(fw!=null)
    try {
     fw.close();
    } catch (IOException e) {
     
     e.printStackTrace();
    }
   if(fr!=null)
    try {
     fr.close();
    } catch (IOException e) {
     
     e.printStackTrace();
    }
  }
 } }

使用缓冲流:

 public static void main(String[] args) {
FileReader fr=null;
FileWriter fw=null;
BufferedReader br=null;
BufferedWriter bw=null;
try {
fr=new FileReader("e://test.txt");
fw=new FileWriter("e://newFile.txt");
br=new BufferedReader(fr);
bw=new BufferedWriter(fw); String line=null;
while ((line=br.readLine())!=null) {
bw.write(line);
}
} catch (IOException e) { e.printStackTrace();
}finally{
try {
//要关闭缓冲流,这样数据才会被刷新然后写入到新的文件中
bw.close();
br.close();
} catch (IOException e) { e.printStackTrace();
}
} }

最新文章

  1. yii2 的request get pos请求 基本用法示例
  2. display:none 与 opacity:0
  3. SQL Server性能影响的重要结论
  4. 夺命雷公狗---Thinkphp----12之文章的增删改查(图片上传和关联查询)
  5. weblogic部署ssh2应用出现异常
  6. cometd的js端代码
  7. Python 3 集合基础和概念!
  8. linux 内核的futex pi-support,即pi-futex使用rt_mutex委托
  9. Spring DelegatingFilterProxy解析
  10. 阿里深度兴趣网络模型paper学习
  11. Android Stutio中使用java8的Lambda表达式
  12. python 视频转成代码视频
  13. Lintcode105 Copy List with Random Pointer solution 题解
  14. Vue之项目搭建
  15. 解决HTTP status code is not handled or not allowed
  16. mbpoll Test FreeModbus TCP Demo
  17. MySql union与order by
  18. 详细解读大数据分析引擎Pig&PigLatin语句
  19. Parted 手册
  20. SPI中的极性CPOL和相位CPHA

热门文章

  1. 卸载 Windows 8/8.1/10 无法常规卸载的内置应用
  2. git 忽略文件.gitignore
  3. MVC Pager使用
  4. 最近公共祖先 LCA Tarjan算法
  5. tp5 $this/Db
  6. EF配置文件初始化数据库 codefirst
  7. RAW编程接口
  8. Web测试实践--Rec 3
  9. [GO]使用map生成 json
  10. C# 静态类的使用