不用读和写的方式可API 中也没有关于文件拷贝的函数请高手能够指点一下

解决方案 »

  1.   

    用filechannel
    其中有一个叫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 ");
    }

    }
    }
      

  2.   

    import java.io.BufferedReader;
    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");
        }}