java 读取TXT文件

读取文件需要注意的主要是获取数据的判空与对数据的处理,数据类型的确认(全量还是增量),异常的分类处理,流的关闭(非常重要),数据的备份,注释的添加

然后txt文件就直接用InputStreamReader读取就好了,没什么特别的.

-------------下面参考代码

 

package com.fndsoft.schedule.service.impl;

import com.fndsoft.common.utility.constant.SysTemparameterConstant;
import com.fndsoft.common.utility.entity.SysTemparameterEO;
import com.fndsoft.common.utility.service.SysTemparameterService;
import com.fndsoft.schedule.model.Accounting;
import com.fndsoft.schedule.dao.AccountingrelationDao;
import com.fndsoft.schedule.service.AccountingrelationService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;

/**
 * 核算机构与报账的处理job Sercice实现类
 * Created by zcp on 2017/9/6.
 */
@Service
public class AccountingrelationServiceImpl implements AccountingrelationService {

    private static final Logger logger = LoggerFactory.getLogger(AccountingrelationServiceImpl.class);
    @Autowired
    private AccountingrelationDao accountingrelationDao;
    @Autowired
    private SysTemparameterService sysTemparameterService;

    @Override
    public void saveToAccounting() {

        //获取文件读取路径与文件备份路径
        SysTemparameterEO receivePathEO = sysTemparameterService.getUrlMap().get(SysTemparameterConstant.P07FILERETURN);
        String receivePath = receivePathEO.getCode();
        SysTemparameterEO backUpPathEO = sysTemparameterService.getUrlMap().get(SysTemparameterConstant.P07FILEBACKRETURN);
        String backUpPath = backUpPathEO.getCode();

        InputStreamReader read = null;
        BufferedReader brReader = null;
        String lineStr = null;
        String[] line = null;

        try {
            File files = new File(receivePath);
            logger.info("路径:"+receivePath);
            File subFile[] = files.listFiles();
            if (null == subFile || subFile.length == 0) {
                logger.info("没有文件");
                return;
            }
            for (File file : files.listFiles()) {
                String filename = file.getName();
                logger.info("有文件:"+filename);
                if (filename.toLowerCase().startsWith("p07top09fk_comorg_") && filename.toLowerCase().endsWith(".txt")) {
                    logger.info("开始处理文件:" + receivePath + ":" +filename);
                    //全量数据(全删全插操作)
                    accountingrelationDao.deleteAccountingInfo();
                    Accounting accounting = null;
                    //GBK格式读取
                    read = new InputStreamReader(new FileInputStream(file), "GBK");
                    brReader = new BufferedReader(read);
                    logger.info("开始解析文件");
                    if(file.exists() && file.length() != 0) {
                        int count=0;
                        //读取一行数据是否为空
                        while ((lineStr = brReader.readLine()) != null) {
                            /***
                             * split(,-1)是表示读取到一次分割符做一次分割,默认的写法读到多个在一起的分隔符做一次分割
                             * 这里加双斜线是转义,一层java代码转义,一层正则表达式转义(个人理解)
                             * 1:"|","*","+"等字符加双斜线转义->"\\"
                             * 2:"\"加三个斜线"\\\",也就是说要写成"\\\\"
                             */
                            line = lineStr.split("\\|",-1);
                            if (line != null && line.length > 1) {
                                ++count;
                                try {
                                    //this.convertTxtOneLine该方法解析读取的数据
                                    accounting = this.convertTxtOneLine(line);
                                        //这里判空,根据自己情况修改逻辑
                                        if (StringUtils.isNotBlank(accounting.getCountingOrgCode()) && StringUtils.isNotBlank(accounting.getCountingOrgName()) && StringUtils.isNotBlank(accounting.getReimbursementCode()) && StringUtils.isNotBlank(accounting.getReimbursementName())) {
                                            accountingrelationDao.saveAccountingInfo(accounting);
                                        }
                                } catch (Exception e) {
                                    //单行异常打印并继续执行,当然这里可以加个记录表更好
                                    logger.info("处理出错"+e.getMessage()+ "count:"+count,e);
                                    continue;
                                }
                            }
                        }
                    }
                    //正在使用中文件关闭
                    if (brReader != null) {
                        try {
                            brReader.close();
                        } catch (IOException e) {
                            logger.info("使用中文件关闭失败",e);
                        }
                    }
                    if (read != null) {
                        try {
                            read.close();
                        } catch (IOException e) {
                            logger.info("read流关闭失败",e);
                        }
                    }
                    //备份,将文件移动到被分文件,如果在文件修改下文件名则更好
                    FileUtils.copyFileToDirectory(file, new File(backUpPath));
                    logger.info("备份路径:"+backUpPath);
                    if (file.exists()) {
                        file.delete();
                    }

                }
            }
        } catch (Exception e) {
            //这里没处理不完善,正确应该是分情况处理异常的,io异常,运行是异常,分开捕获处理
            logger.info("操作失败",e);
        } finally {
            //流单个关闭
            if (brReader != null) {
                try {
                    brReader.close();
                } catch (IOException e) {
                    logger.info("关闭流失败",e);
                }
            }
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    logger.info("关闭流失败",e);
                }
            }
        }
    }
    //对数据简单处理
    public Accounting convertTxtOneLine(String[] line){
        Accounting oneLine = new Accounting();
        if (line[0] != null && !"".equals(line[0])){
            oneLine.setCountingOrgCode(line[0]);
        }
        if (line[1] != null && !"".equals(line[1])){
            oneLine.setCountingOrgName(line[1]);
        }
        if (line[2] != null && !"".equals(line[2])){
            oneLine.setReimbursementCode(line[2]);
        }
        if (line[3] != null && !"".equals(line[3])){
            oneLine.setReimbursementName(line[3]);
        }
        return oneLine;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值