欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读取txt文件

程序员文章站 2024-03-23 21:02:40
...

读取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;
    }
   
	   
   }
相关标签: java