不用读和写的方式可API 中也没有关于文件拷贝的函数请高手能够指点一下
解决方案 »
- 求java高手 帮忙解决一下问题,给定对象的具体属性值,从一个容器,取出具体对象的算法
- 我想实现JAVA串口通信并把得到的数据画一个坐标显示出来连成折线或曲线
- 鼠标怎么样或得JTable某行某列的值,该用那个事件?
- 按钮事件中引入线程的问题——大家测试一下这个程序出错在哪里?
- java一道面试问题,在线等 急
- ======编码问题,我要死了,散分救命!======
- 那位有《java技术内幕》的电子书,高分相送!!
- 基础问题java.lang.NoSuchMethodError: main什么意思啊?
- java Socket问题求救!!师兄师姐们救命阿!!!!
- 我想调用com.sun.java.swing.*包中的类,如JFrame,JButton,是不是要先安装JFC?哪里有,怎么装?
- 如何单击按钮时,显示内容?
- 面试题目请大家指点
其中有一个叫transferto()的方法
利用通道进行快速复制
如:
package test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;public class Copyfile {
public static void main (String args[])
{
File f= new File ("D://eMule-0.47c-VeryCD1215-Setup.exe");
try {
FileInputStream fin = new FileInputStream (f);
FileChannel fc = fin.getChannel();
FileOutputStream fout = new FileOutputStream("D://1.exe");
WritableByteChannel to = fout.getChannel();
fc.transferTo(0, fc.size(),to );
} catch (FileNotFoundException e) {
System.err.println("File not found!");
} catch (IOException e) {
System.err.println("there are errors on processing ");
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class CopyFile { /**
* @param args
*/
public void copy(String from,String to){
File fileFrom = new File(from);
File fileTo = new File(to);
if(!fileTo.exists()){
try {
fileTo.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!fileFrom.canRead()){
System.out.println("源文件为受保护文件!不可以作写出操作!");
System.exit(0);
}
if(!fileTo.canWrite()){
System.out.println("目标文件为只读文件!");
System.exit(0);
}else{
try{
FileInputStream in = new FileInputStream(fileFrom);
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileTo)));
String temp = buff.readLine();
while(temp != null){
out.write(temp);
out.flush();
temp = buff.readLine();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
CopyFile cf = new CopyFile();
cf.copy("E:\\a.txt","E:\\b.txt");
}}