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; } }
相关文章推荐
- java读取.txt文件的最后一行
- php 读取txt文件中的内容,转换成数组
- 运用JAVA读取txt文件
- c# 读取文件内容存放到int数组 array.txt
- C++读取以空格作为数据区分标记,以回车为行标记的txt文件到一个整数数组(字符串妙用)
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法
- Java读取txt文件,计算2011年9月份的通话时间
- 运用JAVA读取txt文件(BufferReader)
- Java读取文件内容并转换为字符串
- Java读取远程服务器上的txt文件
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 怎么从一个txt格式的文件中一次读取10个值并赋值给一个数组???
- JAVA如何实现读取文件最后一行记录,在文件的结尾追加记录
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- 请教Ado.Net按文本读取CSV/Txt文件时,如何禁止将内容转换成数字
- c#读取文件内容存放到int数组 array.txt
- 比如这个input.txt文件中的字符串:...把他们去掉那些空格,标点,换行符等,读取到数组word[MAX]中。
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法