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

该博客展示了如何使用Java从TXT文件中读取数据,并将其转换为数组。通过BufferedReader和FileInputStream实现文件读取,使用split方法根据空格拆分字符串转化为数组,同时提供了针对特定节点数据的读取功能。
摘要由CSDN通过智能技术生成

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> nodeDatas = readEFile(eFilePath, node);

for (int i = 0; i < nodeDatas.size(); i++) {

List 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>返回节点中的数据

*/

public static List> 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> listDatas = new ArrayList>();

while ((line = reader.readLine()) != null && flag == false) {

thisIndex++;

if(line.startsWith("

startIndex = thisIndex;

}

else if(line.startsWith(""+node)){

endIndex = thisIndex;

flag = true;

}

else if(startIndex != 0){

String[] split = line.split("\\s+");

List lineDatas = new ArrayList(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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值