zoukankan      html  css  js  c++  java
  • 从一系列的图片文件夹中随机抽取图片

    最近采集了15万张图片,每天采集的图片存储在一个新的目录下,在测试时需要从所有文件夹中随机抽取图片做测试。

    package com.vfsd.core;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class RandSelectPicFromFolder {
        
        public static void main(String[] args) {
            String folderPath = "H:\PicDir\";
            listFilesFromFolder(folderPath);
        }
        
        public static void listFilesFromFolder(String foldName) {
            File folder1 = new File(foldName);
            File[] files1 = folder1.listFiles();
            for(File indexFile:files1) {
                if(indexFile.isDirectory()) {
                    System.out.println("folder:"+indexFile.getName());
                    selectPicFromFolder(indexFile.getAbsolutePath());
                }
                
                //if(indexFile.isFile()) {
                //    System.out.println("file:"+indexFile.getName());
                //    //
                //}
            }
        }
        
        public static void selectPicFromFolder(String folderPath) {
            File folder1 = new File(folderPath);
            File[] files1 = folder1.listFiles();
            
            int picNum = files1.length;
            
            int selectPicIndex = (int) (Math.random()*picNum);
            
            System.out.println(selectPicIndex);
            
            File selectFile = files1[selectPicIndex];
            //System.out.println("file:"+selectFile.getName());
            
            String oriFileName = selectFile.getAbsolutePath();
            String newFileName = "H:\select_pic\"+selectFile.getName();
            
            System.out.println(oriFileName+"  "+newFileName);
            
            try {
                copyFile(oriFileName,newFileName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public static void copyFile(String oriFilePath,String newFilePath) throws IOException{
            File oriFile = new File(oriFilePath);
            File newFile = new File(newFilePath);
            
            FileInputStream fis = new FileInputStream(oriFile);
            FileOutputStream fos = new FileOutputStream(newFile);
            
            byte[] bytes = new byte[1024];
            int b=0;
            while((b=fis.read(bytes))!=-1) {
                fos.write(bytes, 0, b);
            }
            
            fos.flush();
            fis.close();
            fos.close();
            
            
        }
    
    }

    QQ 3087438119
  • 相关阅读:
    考研打卡_Day018
    如何使用python中的pymysql操作mysql数据库
    Linux系统目录结构和常用目录主要存放内容的说明
    MySQL基础入门使用和命令的使用
    Python中property属性的概论和使用方法
    如何有效的优化自己的网站访问速度
    机器学习中的特征工程学习
    ffmpeg中c语言sdk多媒体互转主要使用的api
    FFmpeg使用c语言sdk实现打印视频的信息
    ffmpeg使用C语言sdk实现抽取视频中的视频数据
  • 原文地址:https://www.cnblogs.com/herd/p/13342117.html
Copyright © 2011-2022 走看看