day03

输入输出流:读入写出
 
 节点流:
   有明确的来源和去向
   往往对字节操作
 节点流又叫低级流、字节流
 
  处理流:
  没有明确的来源和去向
  往往对低级流或其他高级流进行操作,不能独立存在
  处理流又叫高级流

*FileOutputStream fos=new FileOutputStream("a.txt",true);     //创建对象,在当前目录下创建一个名为“a”的txt文件

//FileOutputStream默认会覆盖原有的内容 若不想被覆盖,声明对象时添加一个参数true

*  String message="断剑重铸之日,其势归来之时。";

*  fos.write(message.getBytes());

*fos.close()                           //记得关闭流,否则可能会造成严重后果

例:用字节流来复制某个文件

*FileInputStream fis1=new FileInputStream("文件名.文件类型");                  //读入
*  long ms1=System.currentTimeMillis();
*  byte []buf1=new byte[文件大小(单位b)];
*  fis1.read(buf1);
*  FileOutputStream fis2=new FileOutputStream("复制的文件文件名.文件类型");            //写出  
*  fis2.write(buf1);
*  long ms2=System.currentTimeMillis();
*  System.out.println(ms2-ms1);                                //用时(毫秒)
*  fis2.close();

BufferedInputStream/BufferedOutputStream:

缓冲高级流。内部维护了一个缓冲区,当我们需要写出数据时,会将数据存放在缓冲区,当缓冲区满时,一次性将数据写出。高级流需要用要用低级流作为辅助。

*FileOutputStream fos=new FileOutputStream("b.txt");        //低级流(字节流)

*BufferedOutputStream bos=new BufferedOutputStream(fos);      //高级流  (两者都为写出流)

*  String message="德玛西亚之力";                
 
* byte[]buf=message.getBytes();                  //将字符串转换为字节,用数组接收
*  bos.write(buf);                         //写出                 

*  bos.close();                          //关闭高级流即可

用高级流复制文件:

  FileInputStream fos = new FileInputStream("文件名.文件类型");
  BufferedInputStream bos = new BufferedInputStream(fos);
  
  byte[] b = new byte[10240];                     //可以不调用数组进行复制
  
  FileOutputStream fis = new FileOutputStream("复制出的文件文件名.文件类型");
  BufferedOutputStream bis = new BufferedOutputStream(fis);
  
  long a1 = System.currentTimeMillis();
  int i = -1;
  while ((i = bos.read(b)) != -1) {              //(i = bos.read()) != -1 此注释与数组相关
   bis.write(b);                    
   //bis.write(i);
  }
  bis.close();
  bos.close();
  long a2 = System.currentTimeMillis();
  System.out.println(bs - a);              //计算用时(单位毫秒)

最新文章

  1. Java开发中的23种设计模式详解(转)
  2. 三言两语之微信小程序开发初体验(1)
  3. jquery获取所有被选中checkbox
  4. SWING
  5. string.format
  6. 什么是WebService
  7. Ci 分页类的所有属性总结
  8. 自制单片机之十一……模数转换IC ADC0809
  9. screen 链接远程桌面
  10. python map filter reduce的优化使用
  11. laravel5.5通过Migrations修改表 的artisan命令
  12. H5选择颜色-前端颜色选择器
  13. Codeforces 772A Voltage Keepsake - 二分答案
  14. ListView与ArrayAdapter(二)
  15. 3、Ansible playbooks(Hosts、Users、tasks、handlers、变量、条件测试(when、迭代)、templates)
  16. Java三种代理模式:静态代理、动态代理和cglib代理
  17. https://www.cnblogs.com/hnxxcxg/p/6085149.html
  18. 1.AKATSUKI
  19. linux上mysql安装详细教程
  20. POJ:Dungeon Master(三维bfs模板题)

热门文章

  1. ios中陀螺仪CoreMotion的用法
  2. openstack网络(一)
  3. [TimLinux] myblog 首页创建
  4. 2019CCPC秦皇岛 J MUV LUV EXTRA(KMP)
  5. 笔记||Python3之列表与元组
  6. 面试题-关于Java线程池一篇文章就够了
  7. rails 创建项目、创建controller、model等
  8. Vue中router路由异步加载组件-优化性能
  9. CentOS 7.4搭建LAMP,LAMP:Linux、Apache、MySQL、PHP
  10. 带你使用Visual Studio 2019创建一个MVC Web应用