public static boolean CopyFile(java.io.File filefrom, java.io.File fileto,
                                 boolean rewrite) {
    if (!filefrom.exists()) {
      System.out.println("文件不存在");
      return false;
    }
    if (!filefrom.isFile()) {
      System.out.println("不能够复制文件夹");
      return false;
    }
    if (!filefrom.canRead()) {
      System.out.println("不能够读取需要复制的文件");
      return false;
    }
    if (!fileto.getParentFile().exists()) {
      fileto.getParentFile().mkdirs();
    }
    if (fileto.exists() && rewrite) {
      fileto.delete();
    }
    /*
         if (!fileto.canWrite()) {
      System.out.println("不能够写将要复制的文件" + fileto.getPath());
      return false;
         }*/
    try {
      java.io.FileInputStream fosfrom = new java.io.FileInputStream(filefrom);
      java.io.FileOutputStream fosto = new FileOutputStream(fileto);
      byte bt[] = new byte[1024];
      int c;
      while ( (c = fosfrom.read(bt)) > 0) {
        fosto.write(bt, 0, c);
      }
      fosfrom.close();
      fosto.close();
      return true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }  }
 public static boolean CopyFile(String from, String to) {
    java.io.File filefrom = new java.io.File(from);
    java.io.File fileto = new java.io.File(to);
    return CopyFile(filefrom, fileto, true);
  }

解决方案 »

  1.   

    Runtime.getRuntime.Exec("cp c://a.jsp c://a2.jsp");
      

  2.   

    Jason_guo(梦想难成,努力能成!) 这个代码出错D:\>javac a.java
    a.java:17: cannot resolve symbol
    symbol  : variable getRuntime
    location: class java.lang.Runtime
                    Runtime.getRuntime.Exec("cp example.jsp a2.jsp");
                           ^
    1 error
      

  3.   

    try
        {
          Runtime.getRuntime().exec("cp c://a.jsp c://a2.jsp");
        }
        catch(Exception e){System.out.println(e.getMessage());}
    我改成这样通过了,但是运行出错:CreateProcess: cp c://a.jsp c://a2.jsp error=2
    ??????
      

  4.   

    Runtime.getRuntime.Exec("cmd copy c://a.jsp c://a2.jsp");
      

  5.   

    Jason_guo(梦想难成,努力能成!) :编译通过,运行正常,但没有生成出a2.jsp?????????
      

  6.   

    晕,还是我写一个JAVA文件,你等等
      

  7.   

    没这么复杂吧!!/*
    文件的读写,把一个文件的内容写到另一个文件中
    使用方法是:
    编译文件 javac FileWriteRead.java
    运行程序 java 源文件名 目标文件名
    @author Wang Draco
    Date 2004-07-13
    */
    import java.io.*;
    class FileWriteRead {
    public static void main(String args[]) throws IOException{
    int i;
    FileInputStream fin;
    FileOutputStream fout;
    try {
    // open input file
    try {
    fin = new FileInputStream(args[0]);
    } catch(FileNotFoundException e) {
    System.out.println("Input File Not Found");
    return;
    }
    // open output file
    try {
    fout = new FileOutputStream(args[1]);
    } catch(FileNotFoundException e) {
      System.out.println("Error Opening Output File");
    return; }
    } catch(ArrayIndexOutOfBoundsException e) {
    System.out.println("Usage: CopyFile From To");
    return;
    }
    // Copy File
    try {
    do {
    i = fin.read();
    if(i != -1) fout.write(i);
    } while(i != -1);
    } catch(IOException e) {
    System.out.println("File Error");
    }
    fin.close();
    fout.close();
    }
    }
      

  8.   

    这个方法就是在DOS中打,可是如果我不知道盘符,只知道是和这个运行的JAVA文件在同一个路径下,那又该如何写呢?
      

  9.   

    draco2002(Draco) ,IO很费时,能省则省
      

  10.   

    /**
     * 程序编写:jason guo
     * 个人网站:http://osdev.m165.com
     *
     * faq author:raul2002 (raul) 
     * faq url:http://community.csdn.net/Expert/topic/3171/3171740.xml?temp=.7026331
     * 要求:
     * 比如,在 C:\ 有一个文件 a.jsp ,怎样能在同目录下再复制一个,文件名为a2.jsp?
     */
    import java.lang.*;
    public class OSDEV_File01{
    public static void main(String args[])
    {
    try{
    Process pro = Runtime.getRuntime().exec("cmd /c copy c:\\a.jsp c:\\a2.jsp");
    }
    catch(Exception ex)
    {
    System.out.println("error");
    }
    }
    }