读取一个文件的内容,然后写入另外一个文件

public class NioTest4 {

    public static void main(String[] args) throws  Exception {
FileInputStream inputStream = new FileInputStream("input.txt"); FileOutputStream outputStream = new FileOutputStream("output.txt"); FileChannel inputChannel = inputStream.getChannel();
FileChannel outputChannel = outputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true){
buffer.clear();
int read = inputChannel.read(buffer);
if( -1 == read){
break;
}
buffer.flip(); outputChannel.write(buffer); }
inputChannel.close();
outputChannel.close(); } }

  通过NIO读取文件涉及3个步骤

1、从FileInputStream获取FileChannel对象

2、创建Buffer

3、将数据从Channel读取到Buffer中

  绝对方法与相对方法的含义

1、相对方法: limit值与position值会在操作时被考虑到

2、绝对方法: 完全忽略调limit值和position值。

最新文章

  1. three.js笔记
  2. Redis安全访问
  3. POJ3041Asteroids(最小点覆盖+有点小抽象)
  4. BZOJ3823 : 定情信物
  5. UltraEdit20 注册
  6. java使用ms-dos编译,运行程序
  7. Android开发:最详细的 NavigationDrawer 开发实践总结
  8. 学习笔记 之--AJAX核心对象 XMLHttpRequest
  9. JavaWeb验证码的使用
  10. Redis服务器搭建
  11. NNSZ OIers' Blog Archive
  12. js动态加载js css文件,可以配置文件后辍,防止浏览器缓存
  13. php5.5.7添加pgsql,pdo_pgsql,swoole
  14. python数据类型--set(集合)
  15. LR 两种html与url录制
  16. [C++]指针与引用(定义辨析)
  17. pyhon 之 数据类型详解
  18. webpack 学习
  19. day 19 类的名称空间,组合,派生
  20. android studio svn 创建分支

热门文章

  1. 85.webpack的安装失败至成功
  2. JavaScript 之 navigator 对象
  3. Cheat Engine 指针
  4. java 获取当前方法的被调用信息(被那个方法那个类那一行调用)
  5. 第一册:lesson 133.
  6. html中对应Word中的字体和字号
  7. Pytorch数据读取详解
  8. .net 异常
  9. SpringBoot——SpringBoot学习记录【一】
  10. wordpress调用tags作为keywords关键词标签