java解析RP文件_java解析文件

遇到两个小坑:

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 file2List(String filePath) throws IOException {

List 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 line,head = null;

for (int i=0; i

Map 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

tmpMap.put(head.get(j), line.get(j));

}

mapList.add(tmpMap);

}

return mapList;

}

@Test

public void test(){

try {

List 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值