您的位置:首页 > 编程语言 > Java开发

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

2014-06-23 22:36 507 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐