java关于(io&nio) 的 文件copy例子
2008-05-08 15:39
316 查看
例1:java关于nio 的 文件copy例子
import java.io.*;
import java.nio.channels.FileChannel;
public class IO2 {
public void copyFile(String file,String copyFile) throws IOException{
try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream(file).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(copyFile).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
}
public static void main(String args[]) throws IOException{
IO2 io =new IO2();
io.copyFile("C://webapps.rar","C://11.rar");
}
}
例2:java关于io 的 文件copy例子
import java.io.*;
import java.nio.channels.FileChannel;
public class IO2 {
public void copyFile(String file,String copyFile) throws IOException{
int i;
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(copyFile);
do{
i = in.read();
if(i!=-1){
out.write(i);
// System.out.println(i);
}
}while(i!=-1);
out.close();
in.close();
}
public static void main(String args[]) throws IOException{
IO2 io =new IO2();
io.copyFile("C://webapps.rar","C://11.rar");
}
}
import java.io.*;
import java.nio.channels.FileChannel;
public class IO2 {
public void copyFile(String file,String copyFile) throws IOException{
try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream(file).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(copyFile).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
}
public static void main(String args[]) throws IOException{
IO2 io =new IO2();
io.copyFile("C://webapps.rar","C://11.rar");
}
}
例2:java关于io 的 文件copy例子
import java.io.*;
import java.nio.channels.FileChannel;
public class IO2 {
public void copyFile(String file,String copyFile) throws IOException{
int i;
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(copyFile);
do{
i = in.read();
if(i!=-1){
out.write(i);
// System.out.println(i);
}
}while(i!=-1);
out.close();
in.close();
}
public static void main(String args[]) throws IOException{
IO2 io =new IO2();
io.copyFile("C://webapps.rar","C://11.rar");
}
}
相关文章推荐
- java关于(io&nio) 的 文件copy例子
- 关于写入文件缓存时报"java.io.FileNotFoundException open failed (is a director)"错误的解决方法
- Java NIO——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- java IO & NIO
- java IO文件操作简单基础入门例子,IO流其实没那么难
- 2.5在Eclipse里面编译samples/Cpp/里面的几个例子,会出错 "Unable to execute dex: java.nio.BufferOverflowException"
- Java IO模型&NIO
- 关于java文件编译后,同一个文件出现"$"的class
- java_croe 学习笔记之新IO---java.nio 之内存映射文件
- 关于在Java中使用request.getRequestDispatcher(".....")引起的css文件无效或图片无效问题。
- java IO文件操作入门经典例子
- Java文件读写IO/NIO及性能比较总结
- 【Java IO】NIO复制文件
- 黑马程序员--javaIO文件copy
- JAVA对于TXT文件读写-IO操作例子
- JAVA IO分析三:IO总结&文件分割与合并实例
- java:IO-一个文件读写例子
- [Java][IO]JAVA NIO之浅谈内存映射文件原理与DirectMemory
- java 输入/输出流小记,文件copy的例子
- 关于这几天学习的java文件IO