Copy the file with the specified suffix in the single-level folder and rename the file

Idea: First, filter out the files that meet the requirements in the original folder, and then copy these files to the target folder and rename them.

Found: Many bloggers filter out the files with the specified suffix from the original folder through the filter, and then write an enhanced for loop to copy these files to the target folder. Such code will be carried out in the following method one Show. But in fact, we know that when using the listFiles () method as a filter, the File object itself has been traversed, so it can be copied directly after filtering, without the help of an enhanced for loop, such code will be in the following method two To show.

method one:

public static void main(String[] args) {
    copySingleFolder("d:/pic2/a/","d:/pic",".jpg");
}
	
public static void copySingleFolder(String destPath,String srcPath,String suffix) {
    //把两个文件路径转成文件对象并创建目标文件夹
    File srcFile = new File(srcPath);
    File destFile = new File(destPath);
    destFile.mkdirs();
    //通过过滤器筛选原文件夹中指定后缀的文件
    File[] listFiles = srcFile.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return new File(dir,name).isFile()?name.endsWith(suffix):false;
	}		
    });
    //拷贝前通过增强for循环对筛选后的文件进行遍历
    if(listFiles!=null && listFiles.length>0) {
        for(File f:listFiles) {
    //通过字节缓冲流进行拷贝并重新命名(也可以将其封装成静态方法然后直接调用)
            try(
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath+System.nanoTime()+".jpg"));
						) {
	        byte[] bs = new byte[1024];
		int len;
		while((len=bis.read(bs))!=-1) {
		    bos.write(bs,0,len);
		}
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
        }		
    }
    System.out.println("拷贝完成");
}

Method Two:

public static void main(String[] args) {
    copySingleFolder("d:/pic1/a/","d:/pic",".jpg");
}
	
public static void copySingleFolder(String destPath,String srcPath,String suffix) {
    //把两个文件路径转成文件对象并创建目标文件夹
    File srcFile = new File(srcPath);
    File destFile = new File(destPath);
    destFile.mkdirs();
    //通过过滤器筛选原文件夹中指定后缀的文件
    File[] listFiles = srcFile.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
	    File f = new File(dir,name);
	    if(f.isFile()) {
	        if(name.endsWith(suffix)) {
    //通过字节缓冲流进行拷贝并重新命名(也可以将其封装成静态方法然后直接调用)
		    try(
		            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()));
		            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath+System.nanoTime()+".jpg"));
		            ){
		        byte[] bs = new byte[1024];
			int len;
			while((len=bis.read(bs))!=-1) {
			    bos.write(bs,0,len);
			}
		    } catch (IOException e) {
			e.printStackTrace();
		    }
		    return true;
		}
	    }
	    return false;
	}
			
    });
    System.out.println("拷贝完成");	
}

 

Published 4 original articles · received 1 · views 187

Guess you like

Origin blog.csdn.net/joeychiang/article/details/105470054
Recommended