java.nio将一个文件的内容写入到另一个的文件简单例子
2018-01-04 14:59
585 查看
/**
* 将数据从一个通道复制到另一个通道或从一个文件复制到另一个文件
* @author Administrator
*
*/
public class ChannelDemo {
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("E://PAGE.txt");
ReadableByteChannel source = in.getChannel();
FileOutputStream out = new FileOutputStream("E://User.txt");
WritableByteChannel destination = out.getChannel();
copyData(source,destination);
source.close();
destination.close();
System.out.println("success");
}
private static void copyData(ReadableByteChannel source,
WritableByteChannel destination) throws IOException {
ByteBuffer buffer = ByteBuffer.allocateDirect(20*1024);
while(source.read(buffer) != -1){
buffer.flip();
while(buffer.hasRemaining()){//剩余可用长度
destination.write(buffer);
}
buffer.clear();
}
}
}
* 将数据从一个通道复制到另一个通道或从一个文件复制到另一个文件
* @author Administrator
*
*/
public class ChannelDemo {
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("E://PAGE.txt");
ReadableByteChannel source = in.getChannel();
FileOutputStream out = new FileOutputStream("E://User.txt");
WritableByteChannel destination = out.getChannel();
copyData(source,destination);
source.close();
destination.close();
System.out.println("success");
}
private static void copyData(ReadableByteChannel source,
WritableByteChannel destination) throws IOException {
ByteBuffer buffer = ByteBuffer.allocateDirect(20*1024);
while(source.read(buffer) != -1){
buffer.flip();
while(buffer.hasRemaining()){//剩余可用长度
destination.write(buffer);
}
buffer.clear();
}
}
}
相关文章推荐
- 读出一个文本文件,并在前面加上行号后,写入到另一个文件的简单例子
- 将一个文件的内容写入到另一个文件
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- java 把一个文件写入到另一个文件中
- java关于读取数据后简单写入一个文件中的总结
- 使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能。并且在界面上有radio 的选择内容也要上传
- Java生成一个exel文件并写入内容
- 【JAVA】【NIO】对系列翻译的总结,一个综合实例的分析,网络服务端接收客户端输入,实时将内容写文件
- Google PB协议学习记录--一个简单的例子--如何编译.proto文件为.java文件
- 020: class, objects and instance: 一个简单的例子,压缩文件中内容的替换
- 一个简单的写入文件内容的小demo
- 利用Java的FileReader类读取文件内容的简单例子
- java IO编程——复制一个文件中的内容到另一个文件
- java通过流把一个文件的内容写到另一个文件中
- java 实现读一个txt文件 写入到另一个txt文件
- 将本地CSV格式文件内容上传到服务器的一个简单例子
- 通过java的io流将本地文件读取到控制台,并将文件内容再次写入另一个文件中
- java复制文件的一个小例子(实现根据图片上的内容给图片重命名)
- java创建一个文件并写入内容
- 使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能。并且在界面上有radio 的选择内容也要上传