java list洗牌

reverse
shuffle
sort
swap
rotate

 

 

package com.vfsd.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ShuffleJPG {

    public static void main(String[] args) {
        String folderName = "G:\\vfsd_img\\rebar2";
        String folderName2 = "G:\\vfsd_img\\rebar3";
        List<String> list_fileNames = getFolderJPGFileName(folderName);
        
        int k=1;
        
        //洗牌
        Collections.shuffle(list_fileNames);
        
        for(String indexFileName:list_fileNames) {
            
            System.out.println(indexFileName);
            
            String oldFilePath = folderName+File.separator+indexFileName;
            String newFilePath = folderName2 + File.separator+k+".jpg";
            
            try {
                
                copyAndRenameFile( oldFilePath, newFilePath);
                
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            k++;
            
        }
        
    }
    
    
    public static void copyAndRenameFile(String oldFilePath,String newFilePath) throws IOException {
        File oldFile = new File(oldFilePath);
        File newFile = new File(newFilePath);
        
        FileInputStream fis = new FileInputStream(oldFile);
        FileOutputStream fos = new FileOutputStream(newFile);
        
        byte[] bytes = new byte[1024];
        int len1=0;
        while((len1=fis.read(bytes))!=-1) {
            fos.write(bytes);
        }
        
        fos.flush();
        fos.close();
        fis.close();
        
    }
    
    /**
     * 获取图片名称
     * @param folderName
     * @return
     */
    public static List<String> getFolderJPGFileName(String folderName){
        List<String> list_name = new ArrayList<String>();
        String[] list_fileNames = null;
        File dir = new File(folderName);
        if(!dir.isDirectory()) {
            return list_name;
        }
        
        list_fileNames = dir.list();
        
        for(String indexFileName:list_fileNames) {
            list_name.add(indexFileName);
        }
        
        return list_name;
    }
    
}

 

 

 

 

 

#############################

 

posted @ 2022-04-09 17:34  西北逍遥  阅读(115)  评论(0编辑  收藏  举报