使用listFiles的FileFilter过滤来查找文件并处理
作者:互联网
package test; import java.io.File; import java.io.FileFilter; public class MyFilter implements FileFilter { @Override public boolean accept(File file) { if(file.isDirectory()) return true; else { String name = file.getName(); if(name.endsWith(".txt") || name.endsWith(".mp4")) return true; else return false; } } }
package test; import java.io.*; public class test3 { public static void main(String[] args) throws IOException { File f=new File("e:\\test"); MyFilter m=new MyFilter(); File[]file= f.listFiles(m); for(File file1:file){ String destFileName=file1.getName().replaceAll(".txt",".java"); moveto(file1,destFileName); } } private static void moveto(File f,String dname) throws IOException { int len=0; FileInputStream in=new FileInputStream(f); File ff= new File("E:\\test1"+"\\"+dname); if(!ff.exists()){ ff.getParentFile().mkdir(); } FileOutputStream outputStream=new FileOutputStream(ff); byte[]bytes=new byte[10]; while ((len=in.read(bytes))!=-1){ outputStream.write(bytes,0,len); } in.close(); outputStream.close(); } }
标签:File,ff,FileFilter,查找文件,listFiles,file,new,public,String 来源: https://www.cnblogs.com/qinyios/p/10992775.html