栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 读取写入json、yaml、计算大文件(500M) md5

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 读取写入json、yaml、计算大文件(500M) md5

package com.example.modelmanager.tools;


import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.example.modelmanager.exception.BizException;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIXDom;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.FileUtils;
import org.yaml.snakeyaml.Yaml;


import java.io.*;

import java.math.BigInteger;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Slf4j
public class ConfigHandleTools {

    public  void copyFile(String source, String dest)  {
        try {
            FileUtils.copyFile(new File(source), new File(dest));
        } catch (IOException e) {
            log.error("ConfigHandleTools-copyFile: {}",e.getMessage());
            throw new BizException("500",e.getMessage());
        }
    }



    public Boolean isExist(String filePath){
        File file = new File(filePath);
        return file.exists();
    }


    public boolean isSameFile(String filePath1, String filePath2, String file1MD5, String file2MD5) {
        boolean result = true;

        File file1 = new File(filePath1);
        File file2 = new File (filePath2);

        if (file1.length() != file2.length()) {
            result = false;
        } else {
//            String file1MD5 = getFileMD5String(getmMessageDigest(),filePath1);
//
//            String file2MD5 = getFileMD5String(getmMessageDigest(),filePath2);

            if (file1MD5 != null && !file1MD5.equals(file2MD5)) {
                result = false;

            }
        }
        return result;
    }



    public String getFileMD5String( String file) {
        try {
            MessageDigest mMessageDigest = MessageDigest.getInstance("MD5");
            long start = System.currentTimeMillis();
            InputStream fis = new FileInputStream(new File(file));

            byte[] buffer = new byte[8192];
            int length = 0;
            while ((length = fis.read(buffer)) != -1) {
                mMessageDigest.update(buffer, 0, length);
            }
            fis.close();
//            return new BigInteger(1, mMessageDigest.digest()).toString(16);
            long end = System.currentTimeMillis();
            log.info("cal md5 cost: {} ms,{}",end-start,file);
            return new String(Hex.encodeHex(mMessageDigest.digest()));
        } catch (IOException e) {
           log.error("ConfigHandleTools-getFileMD5String: {}",e.getMessage());
           throw new BizException("500",e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            log.error("ConfigHandleTools-getFileMD5String{}",e.getMessage());
            throw new BizException("500",e.getMessage());
        }

    }

    //读取json文件
    public String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            return FileUtils.readFileToString(jsonFile,"UTF-8");
//            FileReader fileReader = new FileReader(jsonFile);
//            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
//            int ch = 0;
//            StringBuffer sb = new StringBuffer();
//            while ((ch = reader.read()) != -1) {
//                sb.append((char) ch);
//            }
//            fileReader.close();
//            reader.close();
//            jsonStr = sb.toString();
//            return jsonStr;
        } catch (IOException e) {
            log.error("ConfigHandleTools-readConfigFile: {}",e.getMessage());
            throw new BizException("500",e.getMessage());
        }
    }

    
    public void readJsonAndCreateYaml(String json_url,String yaml_url) {
        try {
            String param = readJson(json_url);
            createYaml(yaml_url,param);
        } catch (Exception ex) {
            log.error("ConfigHandTool - readJsonAndCreateYaml error: {}", ex.getMessage());
            throw new BizException("500", ex.getMessage());
        }
    }

    
    @SuppressWarnings("unchecked")
    public  void createYaml(String yaml_url,String jsonString) throws JsonProcessingException, IOException {
        // parse JSON
        JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
        // save it as YAML
        String jsonAsYaml = new YAMLMapper().writevalueAsString(jsonNodeTree);

        Yaml yaml = new Yaml();
        Map map = (Map) yaml.load(jsonAsYaml);

        createYamlFile(yaml_url, map);
    }

    
    public  void createYamlFile(String url,Map data){
        Yaml yaml = new Yaml();
        FileWriter writer;
        try {
            writer = new FileWriter(url);
            yaml.dump(data, writer);
        } catch (IOException ex) {
            log.error("ConfigHandTool - createYamlFile error: {}", ex.getMessage());
            throw new BizException("500", ex.getMessage());
        }
    }

    
    @SuppressWarnings("resource")
    public  String readJson(String url) throws Exception {
        File file = new File(url);
        Reader fileReader = new FileReader(file);
        BufferedReader bufReader = new BufferedReader(fileReader);
        String message = new String();
        String line = null;
        while ((line = bufReader.readLine()) != null) {
            message += line;
        }
        return message;
    }

    
    @SuppressWarnings("unchecked")
    public  Map yamlToMap(String yaml_url) {
        Map loaded = null;
        try {
            FileInputStream fis = new FileInputStream(yaml_url);
            Yaml yaml = new Yaml();
            loaded = (Map) yaml.load(fis);
        } catch (FileNotFoundException ex) {
            log.error("ConfigHandTool - yamlToMap error: {}", ex.getMessage());
            throw new BizException("500", ex.getMessage());
        }
        return loaded;
    }

    
    @SuppressWarnings("rawtypes")
    public  Map returnMapFromYaml(String yaml_url) {
        YamlReader reader;
        Object object = null;
        Map map = null;
        try {
            reader = new YamlReader(new FileReader(yaml_url));
            object = reader.read();
            map = (Map) object;
        } catch (FileNotFoundException | YamlException ex) {
            log.error("ConfigHandTool - returnMapFromYaml error: {}", ex.getMessage());
            throw new BizException("500", ex.getMessage());
        }
        return map;
    }

    
    @SuppressWarnings("unchecked")
    public  String yamlToJson(String file) {
        Gson gs = new Gson();
        Map loaded = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            Yaml yaml = new Yaml();
            loaded = (Map) yaml.load(fis);
        } catch (FileNotFoundException ex) {
            log.error("ConfigHandTool - yamlToJson error: {}", ex.getMessage());
            throw new BizException("500", ex.getMessage());
        }
        return gs.toJson(loaded);
    }



}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325157.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号