美文网首页
练习:IO图片

练习:IO图片

作者: printf200 | 来源:发表于2020-03-29 12:09 被阅读0次
    1693051776141F26D0F701CE129B088C.png
    
    
    public class ImageInfo {
    
        private String name;
    
        private String creatTime;
    
        private String modifiedTime;
    
        private String length;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getCreatTime() {
            return creatTime;
        }
    
        public void setCreatTime(String creatTime) {
            this.creatTime = creatTime;
        }
    
        public String getModifiedTime() {
            return modifiedTime;
        }
    
        public void setModifiedTime(String modifiedTime) {
            this.modifiedTime = modifiedTime;
        }
    
        public String getLength() {
            return length;
        }
    
        public void setLength(String length) {
            this.length = length;
        }
    }
    
    
    import java.io.*;
    import java.math.BigDecimal;
    import java.math.MathContext;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    
    public class Test {
    
       private static File outFile = new File("D:/test.txt");
    
    
        public static ArrayList<ImageInfo> readfile(String filepath, ArrayList<ImageInfo> list) throws Exception {
            if (list == null) {
                list = new ArrayList();
            }
    
            ImageInfo info = new ImageInfo();
            File file = new File(filepath);
            // 文件
            if (!file.isDirectory()) {
                info.setName(file.getPath());
                list.add(info);
    
            } else if (file.isDirectory()) {
                // 如果是目录, 遍历所有子目录取出所有文件名
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "/" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        String[] aa=readfile.getPath().split("\\\\");
                        String name=aa[aa.length-1];
                        String jpg = getCreateTime(readfile.getPath());
                        info.setName(name);
                        info.setCreatTime(jpg);
                        info.setLength(getSize(readfile));
                        info.setModifiedTime(getModifiedTime_2(readfile.getPath()));
                        list.add(info);
                    } else if (readfile.isDirectory()) { // 子目录的目录
                        readfile(filepath + "/" + filelist[i], list);
                    }
                }
            }
            return list;
        }
    
        public static String  getSize(File file)
        {
            double result=0;
            String unit="字节";
            long length = file.length();
             if(length<1024*1024*1024)
            {
                result=length/1024.0/1024;
                unit="MB";
            }
    
            BigDecimal bigDecimal=new BigDecimal(result+"",new MathContext(3));
            return bigDecimal.doubleValue()+unit;
    
        }
    
        public static String getCreateTime(String filePath){
            String strTime = null;
            try {
                Process p = Runtime.getRuntime().exec("cmd /C dir "
                        + filePath
                        + "/tc" );
                InputStream is = p.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String result;
                while((result = br.readLine()) != null){
                    if(result.endsWith(".JPG")){
                        strTime = result.substring(0,17);
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            //System.out.println("创建时间 " + strTime);
            //输出:创建时间 2009-08-17 10:21
            return strTime;
        }
    
        /**
         * 读取修改时间的方法2
         */
        public static String getModifiedTime_2(String path){
            File f = new File(path);
            Calendar cal = Calendar.getInstance();
            long time = f.lastModified();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            cal.setTimeInMillis(time);
    //        System.out.println("修改时间[2] " + formatter.format(cal.getTime()));
            //输出:修改时间[2]    2009-08-17 10:32:38
    
    
            return formatter.format(cal.getTime());
        }
    
        public static void main(String[] args) throws IOException {
    
            Writer out;
    
            try {
                ArrayList<ImageInfo> readfile = readfile("C:/Users/wth/Desktop/学习计划-第8次课-无人机影像", null);
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile,true), "utf-8"), 10240);
                for (int i = 0; i < readfile.size(); i++) {
                    out.write(
                            "#name"+readfile.get(i).getName()+
                                    "create_time"+readfile.get(i).getCreatTime()
                                    + "modified_time"+readfile.get(i).getModifiedTime()
                                    + "length(MB)"+readfile.get(i).getLength());
    
                    out.write("\r\n");
                }
                out.flush();
                out.close();
    
    
    
                for(int i=0 ; i < readfile.size(); i++) {
                    System.out.println();
                }
            }
            catch (Exception ex) {
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:练习:IO图片

          本文链接:https://www.haomeiwen.com/subject/libyuhtx.html