用以下的代码,有时候能够实现文件移动的功能,有时候实现不了,环境什么都一样,都是fat32的,大家遇到过这种奇怪的现象么,帮忙阿,谢了先 if(newfilename == null || oldFileName == null){
  AlphaLog.appLogger.error("「rename file 」 err:" + newfilename.toString() + "," + oldFileName.toString());
                }
 if(newfilename.trim().length() == 0 || oldFileName.getName().trim().length() == 0) {
                    AlphaLog.appLogger.error("「rename file 」 err:" + "size == zero");
                } if(oldFileName.exists())
                {
                    AlphaLog.appLogger.info(oldFileName.getPath()+" rename to "+newFileName.getPath()); 
                    AlphaLog.appLogger.info("「rename file 」 is_rename:" +oldFileName.renameTo(newFileName));                 }else {
                    AlphaLog.appLogger.error("oldFileName doesnot exist");
                }

解决方案 »

  1.   

    给你指出几个错误:
    1.if(newfilename == null || oldFileName == null){
       AlphaLog.appLogger.error("「rename file 」 err:" + newfilename.toString() + "," + oldFileName.toString());
      }
       既然newfilename 或者oldFileName可能等于null,你怎么能用 newfilename.toString() 或者 oldFileName.toString()?2. 我不知道 调用AlphaLog.appLogger.error()会不会使得程序在调用处退出,但从你的程序看应该是不会退出程序的,那么你三个 if 语句并列,给我的感觉不太好。总结:这段程序写的太差,需要重新修改。
      

  2.   

    你有没有判断newFile是否存在? 如果存在就不能成功;oldFile是否正被别的程序使用?是的话也不会成功;
      

  3.   

    thanks alot !!
    canot key in chinese ,suddenly!
    as  tangqs(tangqs) said ; this function write too failure,thanks for u comment
    keep care plsto  fox_blue(蓝狐)
    yes ,i will check this,how to ensure the oldfile didnt call out by another function?
    i can make sure the newfile doesn't exist.
      

  4.   

    我也不知道用什么办法能检查某个文件是否被别的进程打开 你调用renameto的时候,最好检查返回的值是true还是false,这样可以进行相应的处理.