读取txt文件

在这里插入图片描述
package com.util.mail;

import java.io.*;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.gingkoo.gal.orm.mybatis.template.MyBatisSessionTemplate;

import com.gingkoo.rpt.dbsvr.server.dao.PmVatOVocMapper;

public class WriteFile {
private static MyBatisSessionTemplate myBatisSessionTemplate;

@Autowired
public void setMyBatisSessionTemplate(MyBatisSessionTemplate myBatisSessionTemplate) {
	this.myBatisSessionTemplate = myBatisSessionTemplate;
}
 private static String speaceMx="XN DESC                              ";

    // >17
 
    public List<PmVat2660M> getTxtDate() throws IllegalAccessException, IOException{
 //  public static void main(String[] args) throws IllegalAccessException, IOException, SQLException {
        String result = readString();
        String[] rs = result.split("\r\n");

        List<String> data = new ArrayList<>();
        List<String> allData = new ArrayList<>();
        boolean isReadData = false;
        boolean isCont = false;
         
        for (String r : rs) {
        	char[] chars = r.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] != '-' && chars[i] != ' ') {
                    break;
                }
                //最后一个-
                if (i == chars.length - 1) {
                    isReadData = true;
                    isCont = true;
                }
            }
            //从-下一行开始取值
            if(isReadData&&r.indexOf("-")>0){
            	continue;
            }
            
           //从最后一行为空结束
           if (isReadData && r.equals("")) {
                //结束
                allData.addAll(data);
                data = new ArrayList<>();
                isReadData = false;
            }
           
           if (isReadData) {
                data.add(r);
            }
        }

        List<PmVat2660M> ss=new ArrayList<>();
          
        for (int i = 0; i < allData.size(); i+=2) {
          ss.add(readStr(allData.get(i),allData.get(i+1)));
           System.out.println(ss);
        }
		return ss;
       
         
    /*     
        OperationDB operationDB = new OperationDB();
           for (int i = 0; i < ss.size(); i++) {
    	   PmVatMonthlyReport pm = ss.get(i);	
           //System.out.println(pm);
          // operationDB.addRcorder(pm);   
         
	}*/
       
    }
     
    public static PmVat2660M readStr(String line1, String line2) throws IllegalAccessException {
    	PmVat2660M s=new PmVat2660M();
       
        char[] chars1 = line1.toCharArray();
        char[] chars2 = line2.toCharArray();
        String space="";
        //获取字段
        Field[] fileIds = PmVat2660M.class.getDeclaredFields();

        int fileIndex=0;

        String value="";
        boolean isReadData=false;
          //循环第一行
        for (int i = 0; i <chars1.length; i++){
            char c=chars1[i];
            if(c == ' '){
            	 space+=c;
                isReadData=false;

                if(StrUtil.isNotBlank(value)){

                    Field fileId = fileIds[fileIndex];
                    fileId.setAccessible(true);
                    fileId.set(s,value);
                    fileIndex++;
                    value="";
                }
            }
            //空格数>最大空格数
            if(space.length()>speaceMx.length()){
                fileIndex++;
                space="";
            }
            if(c != ' '){
                isReadData=true;
            }

            if(isReadData){
                value+=c;
                space="";
            }

            if (i == chars1.length - 1) {
                if(StrUtil.isNotBlank(value)){

                    Field fileId = fileIds[fileIndex];
                    fileId.setAccessible(true);
                    fileId.set(s,value);
                    fileIndex++;
                    value="";
                }
            }
        }



        for (int i = 0; i <chars2.length; i++){
            char c=chars2[i];
            if(isReadData && c == ' ' && space.equals("")){
                space+=c;
                continue;
            }
            if(c == ' '){
                space+=c;
                isReadData=false;

                if(StrUtil.isNotBlank(value)){

                    Field fileId = fileIds[fileIndex];
                    fileId.setAccessible(true);
                    fileId.set(s,value);
                    fileIndex++;
                    value="";
                    continue;
                }
            }
            if(space.length()>speaceMx.length()){
                fileIndex++;
                space="";
            }
            if(c != ' '){
                isReadData=true;
            }

            if(isReadData){
                value+=c;
                space="";
            }

            if (i == chars2.length - 1) {
                if(StrUtil.isNotBlank(value)){
                    Field fileId = fileIds[fileIndex];
                    fileId.setAccessible(true);
                    fileId.set(s,value);
                    fileIndex++;
                    value="";
                }
            }
        }
       
		
        //System.out.println(s);
        return s;
    }
   

    private static String readString() throws IOException {
        File filename = new File("C:/Users/Lemon/Desktop/Sample FM2660M.txt"); 
        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename),"UTF-8"); 
        BufferedReader br = new BufferedReader(reader); 
        String line = "";
        line = br.readLine();
        String str=line;
        while (line != null) {
            line = br.readLine();
            str+=line+"\r\n";
        }
        return str;
    }
   
	   
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值