遇到两个小坑:

1、使用String.split,部分分隔符需要转义:https://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.html

2、读取文件,报文件找不到:http://blog.51cto.com/632977922/1128981

3、java.io.FileNotFoundException: file:/home/admin/trunner/scenarios/64124/63844/target/larkPerf.jar!/data/getGoodsCategory.txt (没有那个文件或目录)

linux和windows文件夹分隔符不一样:

this.getClass().getClassLoader().getResource("data").getPath() + File.separator + "getGoodsCategory.txt"
public class FileUtils {

    public static List<String> file2List(String filePath) throws IOException {
List<String> lines = new ArrayList();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
String line;
while (null != (line=bufferedReader.readLine())){
lines.add(line);
}
return lines;
} /**
* 解析文件,返回mapList
* @param filePath
* @return
* @throws IOException
*/
public static List file2MapList(String filePath) throws IOException {
List list = file2List(filePath);
List mapList = new ArrayList();
List<String> line,head = null;
for (int i=0; i<list.size(); i++){
Map<String, String> tmpMap = new HashMap();
//第一行为表头 (二狗,直接用excel或xml吧,解析得这么费劲)
if (0 == i){
head = Arrays.asList(list.get(i).toString().split("\\|"));
continue;
}
line = Arrays.asList(list.get(i).toString().split("\\|"));
for (int j=0;j<line.size(); j++){
tmpMap.put(head.get(j), line.get(j));
}
mapList.add(tmpMap);
}
return mapList;
} @Test
public void test(){
try {
List<String> list = file2List("D:\\code\\code-test\\lark-perf\\src\\main\\groovy\\cn\\com\\ykse\\perf\\util\\test.txt");
System.out.println(list.get(0));
List list1 = file2MapList("D:\\code\\code-test\\lark-perf\\src\\main\\groovy\\cn\\com\\ykse\\perf\\util\\test.txt");
System.out.println(list1); } catch (IOException e) {
e.printStackTrace();
}
} }

=====2018-09-11补充=========
第3点找不到文件,是打成jar包后运行报错。一个项目中编译运行||引用jar包读取文件,方式不一样
引用jar包并读取该jar包文件时,应该用
this.getClass().getResourceAsStream(fileName)
详情参考:https://www.cnblogs.com/cn-coder/p/7089688.html

java解析文件的更多相关文章

  1. Java解析文件内容

    本文主要实现对.chk文件的解析,将其内容读出来,存入到一个Map中,文件内容实例为: A0500220140828.CHK A05002 |34622511 |373532879 |3 识别分隔符| ...

  2. java解析xml文件并输出

    使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包. ParseXml类代码如下: import java.io.File; import java.util.ArrayLi ...

  3. XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax

    本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...

  4. 用java解析在OpenStreetMap上下载的地图数据(SAX版,适合比较大的xml文件)

    java程序如下: package gao.map.preprocess; import java.io.BufferedWriter; import java.io.File; import jav ...

  5. 使用Java解析XML文件或XML字符串的例子

    转: 使用Java解析XML文件或XML字符串的例子 2017年09月16日 11:36:18 inter_peng 阅读数:4561 标签: JavaXML-Parserdom4j 更多 个人分类: ...

  6. javap -- Java 类文件解析器

    参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...

  7. Java 上传解压zip文件,并且解析文件里面的excel和图片

    需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...

  8. Java解析JSON文件的方法

    http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...

  9. Java解析xml文件遇到特殊符号&会出现异常的解决方案

    文/朱季谦 在一次Java解析xml文件的开发过程中,使用SAX解析时,出现了这样一个异常信息: Error on line 60 of document : 对实体 "xxx" ...

随机推荐

  1. 【并发】基于 @Async和 CompletableFuture 实现并发异步操作

    参考链接:Spring官方示例 User.java package hello; import com.fasterxml.jackson.annotation.JsonIgnorePropertie ...

  2. P1015回文数

    传送 回文数的判断有个神奇的公式: g[i]==g[leng+-i] 其中leng为字符串长度,看每个g[i]是否都满足它,若满足,就是回文数 ps:洛谷的impossible有毒,必须得复制题干中的 ...

  3. 切换了webview 定位不了的解决方法 (还没有试,记录在此)

    # 切换到 webview time.sleep(2) print(driver.contexts) driver.switch_to.context('WEBVIEW_com.tencent.mm: ...

  4. 补充appium -api

    //锁屏 driver.lockScreen(2); //判断是否锁屏 driver.isLocked(); //截屏并保存至本地 File screen = driver.getScreenshot ...

  5. Springboot监控之二:Spring Boot Admin对Springboot服务进行监控

    概述 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控,比如 jvm 监控.类加载.健 ...

  6. [转][C#]WebAPI 必需 Dll

  7. 廖雪峰Java5集合-3Map-Properties的使用

    Properties用于读取配置 properties文件只能使用ASCII码 #表示注释 可以从文件系统读取.properties文件 Properties props = new Properti ...

  8. C语言强化——字符串(2)

    1.将包含字符数字的字符串分开,使得分开后的字符串前一部分是数字后一部分是字母.例 如"h1ell2o3" -> "123hello" #include& ...

  9. [UE4]计算AimOffset偏移动画的角度

  10. java读取按行txt文件

    import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; pub ...