1、 当向某个指定目录的文件写如数据时(比如上传文件,或者新生成某文件),必须先检查整个路径上的目录是否都存在 codes: //文件(带路径) String fileLocation="E://test//test2//test3.txt"; // 查看目录是否存在,不存在就生成 File dir =new File(fileLocation.substring(0, fileLocation.lastIndexOf('//'))); if(!dir.exists()) dir.mkdirs(); 2、复制文件 codes: /** * 拷贝文件 * @param oldFile 被拷贝文件 * @param newFile 新文件 * @return true成功,false失败 */ public static boolean copy(File oldFile , File newFile) { try{ FileInputStream in=new FileInputStream(oldFile); FileOutputStream out=new FileOutputStream(newFile); int len=8192,bytes=0; byte []buffer=new byte[len]; while( (bytes=in.read(buffer))!=-1 ){ out.write(buffer, 0, bytes); } in.close(); out.flush(); out.close(); }catch (Exception e){ e.printStackTrace(); return false; } return true; }