Java封装读取E文件(txt),将一行转换为数组

package com.cimstech.lq.xoa.file;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EFileRead {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String eFilePath = "C:/Users/laiqi/Desktop/接入方案/WorkInfo_20140621_114447.txt";
		String node = "T_MONITOR_STATUS";//monitor status状态监视器
//		String node = "T_DEVICE_FAULT";//device fault设备故障
		
//		String eFilePath = "C:/Users/laiqi/Desktop/接入方案/Microclimate_20140621_114447.txt";
//		String node = "T_ Microclimate";//小气候
		
//		String eFilePath = "C:/Users/laiqi/Desktop/接入方案/MonitoringPoint_20140621_114447.txt";
//		String node = "T_MONITORINFO";//监视

		
		List<List<String>> nodeDatas = readEFile(eFilePath, node);
		for (int i = 0; i < nodeDatas.size(); i++) {
			List<String> lineDatas = nodeDatas.get(i);
			for (int j = 0; j < lineDatas.size(); j++) {
				System.out.print(lineDatas.get(j)+"\t");
			}
			System.out.println();
		}
	}
	/**
	 * 获取指定节点(Node)的数据
	 * @param eFilePath E文件路径
	 * @param node 节点eg:T_MONITOR_STATUS,不需要加<>等
	 * @return List<List<String>>返回节点中的数据
	 */
	public static List<List<String>> readEFile(String eFilePath, String node){
		try {
			int startIndex = 0;
			int thisIndex = 0;
			int endIndex = 0;
			boolean flag = false;
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(eFilePath)));
			String line = reader.readLine();
			List<List<String>> listDatas = new ArrayList<List<String>>();
			while ((line = reader.readLine()) != null && flag == false) {
				thisIndex++;
				if(line.startsWith("<"+node)){
					startIndex = thisIndex;
				}
				else if(line.startsWith("</"+node)){
					endIndex = thisIndex;
					flag = true;
				}
				else if(startIndex != 0){
					String[] split = line.split("\\s+");
					List<String> lineDatas = new ArrayList<String>(Arrays.asList(split));
					lineDatas.remove(0);//删除第一个元素,比如:@、#
					listDatas.add(lineDatas);
				}
			}
			System.err.println(node+"节点标签在第"+startIndex+"-"+endIndex);
			reader.close();
			return listDatas;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

转载于:https://my.oschina.net/qiqitrue/blog/1579117

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值