java读取text&excel测试类

package com.mycim.webapp.vanchip;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.mycim.core.util.StringUtils;
import com.mycim.wip.model.Die;

import jcifs.smb.SmbFile;
import liquibase.util.csv.opencsv.CSVReader;


public class testTextController {

	public static void main(String[] args)
	{
		String readtxtfilepath = "D:\\19C\\T7MC202R6T_01_221205111955map.txt";
		String readExcelfilepath = "D:\\19C\\T7MC202R6T_01_221205111955.xls";
		int n=GetRowCount1(readtxtfilepath);
		System.out.println("共有"+n+"行数据");
		double[][] mypoint = new double[n][2];
		for(int i=0;i<n;i++)
			{
			   mypoint[i][0]=0;
			   mypoint[i][1]=0;
			}
		readCpExcel(readExcelfilepath);
		//ReadTxt1S(readtxtfilepath, mypoint);
		System.out.println("Hello World!");
//		WriteTxt(writetxtfilepath,mypoint);
//		for(int i=0;i<n;i++)
//			System.out.println((i+1)+":["+mypoint[i][0]+","+mypoint[i][1]+"]");
	}

	public static void WriteTxt(String txtPath,double[][] p){    
    	FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(txtPath);//创建文本文件
			for(int i=0;i<p.length;i++)
			{
				if(i == p.length -1) fileWriter.write(p[i][0]+","+p[i][1]);
				else fileWriter.write(p[i][0]+","+p[i][1]+"\r\n");
			}
			fileWriter.flush();
			fileWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     }
	
	public static void ReadTxt1S(String txtPath,double[][] mypoint) //读取txt中的经纬度并赋值给Point数组
    {
        File file = new File(txtPath);
        if(file.isFile() && file.exists())
        {
            try 
            {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line = null;
                int row = 1;
        	    int length;
        	    ArrayList<String> result = new ArrayList<>();
        	    StringBuilder stringBuilder = new StringBuilder();
        	    char achar;
        	    LinkedList<String> lineResult = new LinkedList<>();
        	    int start = 0;
        	    boolean flag;
        	    while ((line = bufferedReader.readLine()) != null) {
        	        if (line.startsWith("-")) {
        	            flag = (start % 2 == 0);
        	            length = line.length();
        	            for (int i = 0; i < length; i++) {
        	                achar = line.charAt(i);
        	                if (Character.isDigit(achar)) {
        	                    stringBuilder.append(achar).append(",").append(row).append(",").append(i + 1);
        	                    if (flag) {
        	                        lineResult.add(stringBuilder.toString());
        	                    } else {
        	                        lineResult.addFirst(stringBuilder.toString());
        	                    }
        	                    stringBuilder.setLength(0);
        	                }
        	            }
        	            row += 1;
        	            start += 1;
        	            result.addAll(lineResult);
        	            lineResult.clear();
        	        }
        	       
        	    }
        	    System.out.println(result.toString());
                bufferedReader.close();
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    }
	
	public static void readCpExcel(String txtPath) 
    {
		
        File file = new File(txtPath);
        Workbook workbook = null;
        if(file.isFile() && file.exists())
        {
            try 
            {
                String line = "";
                String SplitBy = " ";
                String[] Line;
                boolean flag = false;
                List<String[]> BikeDataList = new ArrayList<>();
                try (BufferedReader br = new BufferedReader(new FileReader(txtPath))) {
                int	k = 0;
                    while ((line = br.readLine()) != null) {
                        Line = line.split(SplitBy);
                        BikeDataList.add(Line);
                       // System.out.println("signal "+Line[0]);
                        if (!flag &&Line[0].contains("Serial#")) {
                        	flag = true;
                        	System.out.println("BIN "+Line[0]);
                        	continue;
                        }
                        if (flag) {
                        	if (k < 9){
                        		String liness = Line[1].replaceAll("\t"," ").trim();
                            	String[] array = liness.split(" ");
                            	System.out.println("BIN "+array[2]+" X:"+array[4]+" Y:"+array[5]);
                        	} else {
                        		String liness = Line[0].replaceAll("\t"," ").trim();
                            	String[] array = liness.split(" ");
                            	System.out.println("BIN "+array[2]+" X:"+array[4]+" Y:"+array[5]);
                        	}
                        	k++;
                        }
                        
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            	
            	//readExcel(new FileInputStream(file),file.getName());
            	
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    }

//这个下面是单独函数
public static Workbook readExcel(InputStream is,String extString){
    Workbook wb = null;
    extString = extString.substring(extString.lastIndexOf("."));
    try {
        if(".xls".equals(extString)){
            return wb = new HSSFWorkbook(is);
        }else if(".xlsx".equals(extString)){
            return wb = new XSSFWorkbook(is);
        }else {
            return wb = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return wb;
}

	
	public static void ReadTxt1(String txtPath,double[][] mypoint) //读取txt中的经纬度并赋值给Point数组
    {
        File file = new File(txtPath);
        if(file.isFile() && file.exists())
        {
            try 
            {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line = null;
                int row = 1;
        	    int length;
        	    ArrayList<String> result = new ArrayList<>();
        	    StringBuilder stringBuilder = new StringBuilder();
        	    char achar;
                while( (line=bufferedReader.readLine())!=null){
        	        if (line.startsWith("-")) {
        	            length = line.length();
        	            for (int i = 0; i < length; i++) {
        	                achar = line.charAt(i);
        	                if (Character.isDigit(achar)) {
        	                    stringBuilder.append(achar).append(",").append(row).append(",").append(i+1);
        	                    System.out.println(stringBuilder.toString());
        	                    result.add(stringBuilder.toString());
        	                    stringBuilder.setLength(0);
        	                }
        	            }
        	        }
        	        row +=1;
                }
                bufferedReader.close();
            } catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
    }
	
	public static List<String> dealFile(BufferedReader bufferedReader) throws Exception {
	    int row = 1;
	    String line;
	    int length;
	    ArrayList<String> result = new ArrayList<>();
	    StringBuilder stringBuilder = new StringBuilder();
	    char achar;
	    LinkedList<String> lineResult = new LinkedList<>();
	    int start = 0;
	    boolean flag;
	    while ((line = bufferedReader.readLine()) != null) {
	        if (line.startsWith("-")) {
	            flag = (start % 2 == 0);
	            length = line.length();
	            for (int i = 0; i < length; i++) {
	                achar = line.charAt(i);
	                if (Character.isDigit(achar)) {
	                    stringBuilder.append(achar).append(",").append(row).append(",").append(i + 1);
	                    if (flag) {
	                        lineResult.add(stringBuilder.toString());
	                    } else {
	                        lineResult.addFirst(stringBuilder.toString());
	                    }
	                    stringBuilder.setLength(0);
	                }
	            }
	            start += 1;
	            result.addAll(lineResult);
	            lineResult.clear();
	        }
	        row += 1;
	    }
	    return result;
	}
	

	public static int GetRowCount1(String txtpath)
    {
    	File file = new File(txtpath);
    	int rowcount=0;
        if(file.isFile() && file.exists())
        {
            try 
            {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String text = null;
                while((text = bufferedReader.readLine()) != null)
                {
                	rowcount++;
                }
                bufferedReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
		return rowcount;
    }

    public static String[] split(String lineTxt) {
        String[] arr = lineTxt.split(" ");
        //去除空元素
        List<String> list = new ArrayList<>();
        for (String str : arr) {
            if (StringUtils.isNotEmpty(str)) {
                list.add(str);
            }
        }
        return list.toArray(new String[0]);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值