fundvaluation/src/com/pingan/rbpfunval/ut/FileUtil

package com.pingan.rbpfundval.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pingan.rbpfundval.model.FundValuation;

public class FileUtil {

private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

public static String basicPath; 
public static String filePath;  

public static List<String> produceInitFile(List<String> fundIdList, String strDate) 
        throws IOException {

    List<String> fileNameList = new ArrayList<>();

    if(fundIdList != null && fundIdList.size() > 0){
        StringBuffer sb = new StringBuffer();
        sb.append(fundIdList.size()).append(System.getProperty("line.separator"));
        for (String strFundId : fundIdList){
            sb.append(strFundId);               
            sb.append("\n");
        }

        String valfilename = "fundValuation_init_" + strDate +  ".txt";
        String signfilename = "fundValuation_init_" + strDate + "_sign" + ".txt";
        String okfilename = "fundValuation_init_" + strDate + ".ok";                        
        String pathWithDate = filePath + strDate;
        FileUtil.createDirectory(pathWithDate);
        String filefullname = pathWithDate + "/" + valfilename;
        String signfilefullname = pathWithDate + "/" + signfilename;
        String okfilefullname = pathWithDate + "/" + okfilename;

        FileUtil.creatTxtFile(filefullname);
        FileUtil.writeTxtFile(filefullname, sb.toString());

        String content = FileUtil.getTxtFile(filefullname);
        String signStr = SignUtil.signByRSA(content);
        logger.info("signStr: " + signStr);

        FileUtil.creatTxtFile(signfilefullname);
        FileUtil.writeTxtFile(signfilefullname, signStr);
        FileUtil.creatTxtFile(okfilefullname);

        fileNameList.add(valfilename);
        fileNameList.add(signfilename);
        fileNameList.add(okfilename);
    }

    return fileNameList;        
}

public static List<String> produceValuationFile(List<FundValuation> valuationList, String strDate, String valuationTime, boolean isSupplemented, int iprocessType) 
        throws IOException, InterruptedException {
    String supplementFilename = "";
    List<String> fileNameList = new ArrayList<>();

    if (isSupplemented && iprocessType == 1)
        supplementFilename = "_add";
    if (isSupplemented && iprocessType == 4)
        supplementFilename = "_fix";

    if (valuationList != null && valuationList.size() > 0) {
        StringBuffer fileSB = new StringBuffer();                       
        fileSB.append(valuationList.size()).append("\n");           
        for (FundValuation fv : valuationList) {
            BigDecimal bVal = new BigDecimal(fv.getValuation());
            double valuation = bVal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            BigDecimal bPreVal = new BigDecimal(fv.getPreValuation());
            double preValuation = bPreVal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();

            fileSB.append(fv.getFundId()).append("|");              
            fileSB.append(fv.getValuationTime()).append("|");   
            fileSB.append(String.valueOf(valuation)).append("|");               
            fileSB.append(String.valueOf(preValuation));                
            fileSB.append("\n");
        }

        String timestamp = null;
        if((iprocessType == 0 || iprocessType == 2) && valuationTime!=null && valuationTime.trim().length()>0){
            timestamp = valuationTime;
        }else
            timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());

        String valfilename = "fundValuation_" + timestamp + supplementFilename + ".txt";
        String signfilename = "fundValuation_" + timestamp + supplementFilename + "_sign" + ".txt";
        String okfilename = "fundValuation_" + timestamp + supplementFilename + ".ok";
        String pathWithDate = filePath + strDate;
        FileUtil.createDirectory(pathWithDate);
        String filefullname = pathWithDate + "/" + valfilename;
        String signfilefullname = pathWithDate + "/" + signfilename;
        String okfilefullname = pathWithDate + "/" + okfilename;

        FileUtil.creatTxtFile(filefullname);
        FileUtil.writeTxtFile(filefullname, fileSB.toString());

        //Thread.sleep(10000);

        String content = FileUtil.getTxtFile(filefullname);
        //logger.info("content: " + content);
        String signStr = SignUtil.signByRSA(content);
        logger.info("signStr: " + signStr);

        FileUtil.creatTxtFile(signfilefullname);
        FileUtil.writeTxtFile(signfilefullname, signStr);
        FileUtil.creatTxtFile(okfilefullname);
        //验签测试
        //String strSign = getTxtFile(signfilefullname);
        //boolean b =   SignUtil.verifyByRSA(content, strSign);
        //logger.info("verify result : "+b);

        fileNameList.add(valfilename);
        fileNameList.add(signfilename);
        fileNameList.add(okfilename);

    }

    return fileNameList;
}

/**
 * 创建文件
 * 
 * @throws IOException
 */
public static boolean creatTxtFile(String filefullname) throws IOException {
    boolean flag = false;
    File filename = new File(filefullname);
    if (!filename.exists()) {
        filename.createNewFile();
        flag = true;
    }
    return flag;
}

public static boolean writeTxtFile(String filefullname, String newStr) throws IOException {
    // 先读取原有文件内容,然后进行写入操作
    boolean flag = false;

    FileOutputStream fos = null;
    PrintWriter pw = null;
    try {
        // 文件路径
        File file = new File(filefullname);
        fos = new FileOutputStream(file);
        pw = new PrintWriter(fos);
        pw.write(newStr.toCharArray());
        pw.flush();
        flag = true;
    } catch (IOException e1) {
        // TODO 自动生成 catch 块
        throw e1;
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fos != null) {
            fos.close();
        }
    }
    return flag;
}

public static String getTxtFile(String filefullname) throws IOException {   

    FileInputStream fis = null;
    InputStreamReader isr = null;
    BufferedReader br = null;

    FileOutputStream fos = null;
    PrintWriter pw = null;
    try {
        File file = new File(filefullname);         
        fis = new FileInputStream(file);
        isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);
        StringBuffer buf = new StringBuffer();
        String temp = "";

    for (int j = 1; (temp = br.readLine()) != null; j++) {
        buf = buf.append(temp);                     
    }           
    return buf.toString();

} catch (IOException e1) {
    logger.error(e1.getLocalizedMessage());
    throw e1;
} finally {
    if (pw != null) {
        pw.close();
    }
    if (fos != null) {
        fos.close();
    }
    if (br != null) {
        br.close();
    }
    if (isr != null) {
        isr.close();
    }
    if (fis != null) {
        fis.close();
    }
}

}

public static String getTxt2File(String filefullname) throws IOException {

    FileInputStream fis = null;
    InputStreamReader isr = null;
    BufferedReader br = null;

    FileOutputStream fos = null;
    PrintWriter pw = null;
    try {
        // 文件路径
        File file = new File(filefullname);
        // 将文件读入输入流
        fis = new FileInputStream(file);
        isr = new InputStreamReader(fis);
        br = new BufferedReader(isr);
        StringBuffer buf = new StringBuffer();

        int num = 0;
        char ch;
        while ((num = br.read()) != -1) {
            ch = (char) num;
            buf.append(ch);
        }
        return buf.toString();
    } catch (IOException e1) {
        // TODO 自动生成 catch 块
        throw e1;
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fos != null) {
            fos.close();
        }
        if (br != null) {
            br.close();
        }
        if (isr != null) {
            isr.close();
        }
        if (fis != null) {
            fis.close();
        }
    }
}

public static Object readFileByInputStream(String filePath) {
    ObjectInputStream inputStream = null;
    Object obj = null;
    try {
        inputStream = new ObjectInputStream(new FileInputStream(filePath));
        obj = inputStream.readObject();

    } catch (Exception e) {
        logger.error("file read error", e);
        // throw new CmException("file read error");

    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();

            } catch (IOException e) {
                logger.error("file close error", e);
                // throw new CmException("file close error");
            }
        }
    }
    return obj;
}

public static void createDirectory(String dirName){     
    File file = new File(dirName);
    if (!file.exists()) {
        file.mkdir();
    } 
}

}

转载于:https://blog.51cto.com/7952376/2366313

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值