遇到两个小坑:

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. 通过分析Ajax请求 抓取今日头条街拍图集

    代码: import os import re import json import time from hashlib import md5 from multiprocessing import ...

  2. 在外部怎么调用jquery插件里的function

    文章来源:百度知道 问:(function($){函数(){xxxx}})(jQuery),我怎么调用这个函数呢? (function($){ function render(jq){ 这里是jque ...

  3. [UE4]RPC,远程调用

    RPC 一.Remote Procedure Call:远程程序调用 二.一个进程调用另外一个进程上的函数 由于“Server-shoot”方法被标记为“在服务器上运行”,所以尽管是在第二个窗口(客户 ...

  4. 矩阵半正定: positive semidefinite

    具体定义:https://en.wikipedia.org/wiki/Positive-definite_matrix

  5. windows server 2012 本地用户和组

  6. Round544div3E(1133E)

    一.题目链接 https://codeforces.com/problemset/problem/1133/E 二.思路 显然要使用dp,因为中间有部分人不会选取. 令$dp[i][j]$表示在前$i ...

  7. OpenJudge 求重要逆序对数

    https://blog.csdn.net/mrvector/article/details/81090165 [题解] 方法与求逆序对的个数类似,用归并排序分治求解.不同之处在于添加了一个虚拟指针p ...

  8. Javascript-关于break、continue、return语句

    JS-break:break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch语句.由于它是用来退出循环或者switch语句,所以只有当它出现在这些语句时,这种形式的break语句才 ...

  9. 使用Excel随机函数产生大众评委的6位登录密码

    第一步:填写公式 如下图所示:选中B2单元格输入公式   =TEXT(ROUNDUP(RAND()*1000000,0),"000000") ,然后回车. 可复制的纯文本格式: = ...

  10. (转)C#读写共享文件

    原文地址:https://www.cnblogs.com/guohu/p/4694515.html using System; using System.Collections.Generic; us ...