日期:2014-05-20  浏览次数:20772 次

文件移动的问题,感激不尽!多谢了!
我碰到了一个很奇怪的问题,不知道能不能说清楚,先谢谢了!
public   class   FileMove
{
public   static   boolean   upload(String   localfilename,String   localbak,String   remotefilename)  
{
  try  
  {
    //----------------此处ftp上传代码,能正确上传
    File   file_in   =   new   File(localfilename);
    System.out.println(file_in.toString());
    //此处输出了E:\workzxm\ftpandfilter\ftpdir\test.txt
    File   bak=new   File(localbak);
    System.out.println(bak.toString());
    //此处输出了E:\workzxm\ftpandfilter\ftpbak
    FileMove.move(file_in,bak);
    //此处移动文件失败!什么原因???????????????请大家指点!感激不尽
    return   true;
  }  
  catch   (IOException   ex)  
  {
    System.out.println( "upload   fail ");
    System.out.println(ex);
    return   false;
  }
}
//移除上传后的文件
public   static   boolean   move(File   srcFile,   File   destPath)
{
        System.out.println(srcFile.toString());
        System.out.println(destPath);
        //这两个地方的输出不管是从main调用还是从upload调用都是:E:\workzxm\ftpandfilter\ftpdir\test.txt和E:\workzxm\ftpandfilter\ftpbak
        boolean   success=false;
        try{
        File   f=new   File(destPath,   srcFile.getName());
        System.out.println(f);
                //此处输出不管是从main()调用还是从upload()调用都是E:\workzxm\ftpandfilter\ftpbak\test.txt
        success   =   srcFile.renameTo(f);
        }
        catch(Exception   ex)
        {
        System.out.println(ex);
        }
        return   success;
}
public   static   void   main(String   agrs[])  
{
    String   s1= "E:/workzxm/ftpandfilter/ftpdir/jaxen.jar ";
    File   file_in=new   File(s1);
    String   s2= "E:/workzxm/ftpandfilter/ftpbak ";
    File   bak=new   File(s2);
    FTPToServer.move(file_in,bak);//此处移动文件成功!什么原因???????????????请大家指点!感激不尽
    //也就是说从main调用能成功移动文件,但是从upload调用不能成功移动文件,但是在move函数里面输出的文件都是相同的!!呜呜.........
}
}


------解决方案--------------------
是否与filesystem有关啊
------解决方案--------------------
搂住,FTPToServer.move(file_in,bak);//此处移动文件成功!

FTPToServer是哪里来的呀?

FileMove.move(file_in,bak);
//此处移动文件失败!什么原因???????????????请大家指点!感激不尽
这里是因为ftpbak这个文件夹不存在,所以会失败,我试过了,如果存在的话就可以了。给你个建议,在调用前可以先判断目标文件夹是否存在,不存在先mkdir()建好!

------解决方案--------------------
private void move(File srcFile, File destPath) throws Exception {
File temFile = new File(destPath.getDirectory() + srcFile.getFileName());
if (temFile.exists()) temFile.delete();
sfile.moveTo(destPath);
}
沒試過,自己再試試

------解决方案--------------------
是不是你的原文件已经被移动了,你没看清楚啊