/** * 删除文件,通过文件的类型 * @param filePath * @param type */ public static void deleteType(String filePath,String type){ File file = new File(filePath); if(!file.isDirectory()) return; String[] list = file.list(); if(list==null) return; for(int i=0;i<list.length;i++){ File f = new File(file,list[i]); String panth = f.getPath(); if(f.isDirectory()){ deleteType(panth,type); } if(panth.endsWith(type)){ System.out.println(f.getFreeSpace()+","+panth); //f.delete(); } } file.delete(); } /** * 复制文件 * @param fromFilepath * @param toFilepath */ public static void copyFile(File fromFile,File tofile){ if(tofile!=null&&tofile.isDirectory()){ tofile = new File(tofile,fromFile!=null?fromFile.getName():""); } InputStream inputs =null; OutputStream output =null; try { inputs = new FileInputStream(fromFile); output = new FileOutputStream(tofile); byte[] b = new byte[1024]; int len = inputs.read(b); while(len!=-1){ output.write(b, 0, len); len = inputs.read(b); } //System.out.println("----------copy successful-------------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 复制文件夹 * @param fromFilepath * @param toFilepath */ public static void copyDir(String fromFilepath,String toFilepath){ File fromFile = new File(fromFilepath); File toFile = new File(toFilepath); if(fromFile.isDirectory()&&flag){ toFilepath = toFilepath+"/"+fromFile.getName(); toFile = new File(toFilepath); if(!toFile.exists()) toFile.mkdirs(); flag = false; } String[] list = fromFile.list(); if(list==null) return; for(int i=0;i<list.length;i++){ File file = new File(fromFile,list[i]); if(file!=null&&file.isDirectory()){ toFile = new File(toFilepath,list[i]); if(!toFile.exists()) toFile.mkdirs(); System.out.println("--------"+file.getPath()+"-------------i="+i+"----->"+list[i]); System.out.println("++++++++"+toFile.getPath()+"+++++++++++++"); copyDir(file.getPath(),toFile.getPath()); }else copyFile(file, toFile); } }
文件的复制和删除
猜你喜欢
转载自zhihchen.iteye.com/blog/1500059
今日推荐
周排行