http://java.sun.com/products/jdk/1.2/docs/api/java/io/File.html#renameTo(java.io.File)public boolean renameTo(File dest)

解决方案 »

  1.   

    public boolean renameTo(File dest)
    Renames the file denoted by this abstract pathname.
    Parameters:
    dest - The new abstract pathname for the named file
    Returns:
    true if and only if the renaming succeeded; false otherwise
    Throws:
    SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.io.FileDescriptor) method denies write access to both the old and new pathnames
    NullPointerException - If parameter dest is null
      

  2.   

    String rel=request.getRealPath("/");
    File file1=new File(rel+"txt1.htm");
    File file2=new File(rel+"txt2.htm");
    if(file1.exists()){
       file1.renameTo(file2);
    }
    //把服务器根目录下的txt1.htm改名成了txt2.htm
      

  3.   

    to:xiaofenguser不行,有错误:Full compiler error(s):
    D:\bea2\user_projects\hyf\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_169390\jsp_servlet\_newemail\__upload_ok.java:129: reference to File is ambiguous, both class java.io.File in java.io and class com.jspsmart.upload.File in com.jspsmart.upload match
                    File file1=new File(rel+"cs15.rar"); //[ /newemail/upload_ok.jsp; Line: 55]
                    ^
    D:\bea2\user_projects\hyf\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_169390\jsp_servlet\_newemail\__upload_ok.java:129: reference to File is ambiguous, both class java.io.File in java.io and class com.jspsmart.upload.File in com.jspsmart.upload match
                    File file1=new File(rel+"cs15.rar"); //[ /newemail/upload_ok.jsp; Line: 55]
                                   ^
    D:\bea2\user_projects\hyf\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_169390\jsp_servlet\_newemail\__upload_ok.java:130: reference to File is ambiguous, both class java.io.File in java.io and class com.jspsmart.upload.File in com.jspsmart.upload match
                    File file2=new File(rel+"cs.rar"); //[ /newemail/upload_ok.jsp; Line: 56]
                    ^
    D:\bea2\user_projects\hyf\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_169390\jsp_servlet\_newemail\__upload_ok.java:130: reference to File is ambiguous, both class java.io.File in java.io and class com.jspsmart.upload.File in com.jspsmart.upload match
                    File file2=new File(rel+"cs.rar"); //[ /newemail/upload_ok.jsp; Line: 56]
                                   ^
    Note: D:\bea2\user_projects\hyf\myserver\.wlnotdelete\DefaultWebApp_DefaultWebApp_169390\jsp_servlet\_newemail\__upload_ok.java uses or overrides a deprecated API.
    Note: Recompile with -deprecation for details.
      

  4.   

    both java.io.* and com.jspsmart.upload.* contains File, you shall declare as follows:java.io.File file1=...
    or 
    com.jspsmart.upload.File file1=...
      

  5.   

    你的File类重了,就是上面这位说的,改成:
    String rel=request.getRealPath("/");
    java.io.File file1=new java.io.File(rel+"txt1.htm");
    java.io.File file2=new java.io.File(rel+"txt2.htm");
    if(file1.exists()){
       file1.renameTo(file2);
    }
    就可以了.