我要把a目录下的1.jpg移动到b目录下并且更名为2.jpg.要如何实现呢?我是大概这样写的File oldFile = new file("a//1.jpg");
if (oldFile != null) {
File newFile = new file("b//2.jpg");
if (newFile != null)
return oldFile.renameTo(newFile);
}

解决方案 »

  1.   

    呵 , 如果复制文件就直接New的话 , 那就太方便了。
      

  2.   

    就是这样写.
    判断一下目标文件是否存在就可以了还需要注意的是Android程序只能写SDCard和自己程序的路径(/data/data/<your package>)
      

  3.   

    //直接把a下的1.jpg先copy到b下的2.jpg中(首选创建2.jpg文件),copy完后就把a下的1.jpg删除就OK了
    不要用到renameTo()File a = new File(根目录/a/1.jpg)
    File b = new File(根目录/b/2.jpg)
    if(!b.exists())
    {
      b..createNewFile();
    }
    // 实例输入输出的文件流
    InputStream in = null;
    OutputStream out = null;
    BufferedInputStream inb = null;
    BufferedOutputStream oub = null;
    try {
    // 构建文件输入流
    in = new FileInputStream(a);
    // 构建文件输出流
    out = new FileOutputStream(b);
    inb = new BufferedInputStream(in);
    oub = new BufferedOutputStream(out); // 开始复制
    byte[] read = new byte[1024];
    int len = in.read(read);
    while (len != -1) {
    oub.write(read, 0, len);
    len = in.read(read);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    oub.close();
    inb.close();
    out.close();
    in.close();
    } catch (Exception e0) {
    e0.printStackTrace();
    }
    }a.delete();