这是要用到正则表达式么? 请指点下,最好有代码

解决方案 »

  1.   

    不需要正则表达式
    递归,过滤.java文件,copy到test下
      

  2.   

    import java.io.File;
    import java.io.FilenameFilter;public class Filter implements FilenameFilter { @Override
    public boolean accept(File dir, String filename) {
    // TODO Auto-generated method stub
    return filename.endsWith(".java");
    }}然后
    File filelist = new  File("d:\work");
    String[] dirs = filelist.list(new Filter());取到后随便你放到什么地方
      

  3.   

    递归,过滤.java文件,copy到test下
      

  4.   

    这里只有过滤器的设置,递归的话你就自己写吧 public static void main(String[] args) {
    File file = new File("");
    if(file.isDirectory()){
    file.listFiles(new FileFilter() {
    public boolean accept(File file) {
    String path = file.getPath();
    String ex = path.substring(path.indexOf(".") + 1);
    if(ex.equalsIgnoreCase("JAVA")){
    return true;
    }
    return false;
    }
    });
    }
    }
      

  5.   

    package io;import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FilenameFilter;
    import java.io.IOException;public class FilterAndCopy{ /**
     * @param args
     */
    public static void main(String[] args) {
    FilterAndCopy fc = new FilterAndCopy();
    File[] fileList = fc.findFile("f://",".java");
    fc.copy( fileList, "f://test");
    }
    /**
     * 查找sourceDir目录下后缀为sign的文件
     * @param sourceDir
     * @return
     */
    public  File[] findFile(String sourceDir,final String sign){
    File fileDir = new File(sourceDir);
    if(!fileDir.exists()){
    throw new  RuntimeException("您查找的文件夹不存在");
    }
    File[] fileList = fileDir.listFiles(new FilenameFilter() {

    @Override
    public boolean accept(File dir, String name) {
    // TODO Auto-generated method stub
    return name.endsWith(sign);
    }
    });
    return fileList;
    }
    /**
     * 复制文件
     * @param fileList
     * @param endDir
     */
    public void copy(File[] fileList,String endDir){
    BufferedInputStream binput = null;
    BufferedOutputStream bout = null;
    byte[] b = new byte[1024];
    File endFileDir = new File(endDir);
    if(!endFileDir.exists()){
    endFileDir.mkdir();
    }
    for(File file : fileList){
    try {
    binput = new BufferedInputStream(new FileInputStream(file));
    bout = new BufferedOutputStream(new FileOutputStream(new File(endDir,file.getName())));
    while(binput.read(b) != -1){
    bout.write(b);
    bout.flush();
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    try {
    if(binput != null){
    binput.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {
    if(bout != null)
    bout.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }