美文网首页
练习: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图片

  • IO(计数练习)

    用于记录应用程序运行次数。如果使用次数已到,那么给出注册提示。 很容易想到的是:计数器。可是该计数器定义在程序中,...

  • Python数字图像处理(2):图像的读取、显示和保存

    skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的。为了方便练习,也提供一个data模块,里...

  • typing.io

    typing.io 地址:https://typing.io/ typing.io是一个针对程序代码的打字练习网站...

  • **Swift 枚举 简单实现 二叉搜索树

    练习来自Functional Swift (objc.io)。轻微改动。

  • java——IO_拷贝文件夹

    1、图片的拷贝(io流): 图片,文档,ppt等等的拷贝都是运用了输入流和输出流(io流)来实现 2、拷贝目录下所...

  • 常用网站目录

    图片转图标字体https://icomoon.io/app/#/select

  • 通过Core Image添加滤镜

    by : http://cocacola-ty.github.io 处理过程 读取图片读取UIImage类型图片将...

  • 图片练习

    图片练习 ① 深浅:浅(比较明快的天蓝色) 冷暖:冷 艳柔:柔 ② 上左图 深浅:白色似乎不分辨 裤子...

  • IO(打印流练习)

    打印流:该流提供了打印方法,可以将各种数据类型的数据都原样打印。 字节打印流:PrintStream构造函数可以接...

网友评论

      本文标题:练习:IO图片

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