File src = new File(filePath); move = src.renameTo(new File(targetPath));
还有就是复制文件
public static void copyFile(File src, File target) { //判断是否为文件夹 if (src.isDirectory()) { if (!target.exists()) { target.mkdir(); } // 复制文件夹 File[] currentFiles; currentFiles = src.listFiles(); for (int i = 0; i < currentFiles.length; i++) { // 如果当前为子目录则递归 if (currentFiles[i].isDirectory()) { copyFile(new File(currentFiles[i]+"/"), new File(target.getAbsolutePath() + "/" + currentFiles[i].getName()+"/")); } else { copyFile(currentFiles[i], new File(target.getAbsolutePath() + "/" + currentFiles[i].getName())); } }
} else { // 创建输入输出流 InputStream in = null; OutputStream out = null; // 创建缓存字节流 BufferedInputStream bin = null; BufferedOutputStream bout = null; try { // 创建实例 in = new FileInputStream(src); out = new FileOutputStream(target); bin = new BufferedInputStream(in); bout = new BufferedOutputStream(out);
byte[] b = new byte[8192];// 用于缓存的字节数组 int len = bin.read(b);// 获取读取到的长度 while (len != -1)// 判断是否读取到尾部 { bout.write(b, 0, len); len = bin.read(b); }