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<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解析文件的更多相关文章
- Java解析文件内容
本文主要实现对.chk文件的解析,将其内容读出来,存入到一个Map中,文件内容实例为: A0500220140828.CHK A05002 |34622511 |373532879 |3 识别分隔符| ...
- java解析xml文件并输出
使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包. ParseXml类代码如下: import java.io.File; import java.util.ArrayLi ...
- 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来 ...
- 用java解析在OpenStreetMap上下载的地图数据(SAX版,适合比较大的xml文件)
java程序如下: package gao.map.preprocess; import java.io.BufferedWriter; import java.io.File; import jav ...
- 使用Java解析XML文件或XML字符串的例子
转: 使用Java解析XML文件或XML字符串的例子 2017年09月16日 11:36:18 inter_peng 阅读数:4561 标签: JavaXML-Parserdom4j 更多 个人分类: ...
- javap -- Java 类文件解析器
参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...
- Java 上传解压zip文件,并且解析文件里面的excel和图片
需求:上传一个zip文件,zip文件里面包含一个excel和很多图片,需要把excel里面的信息解析出来保存到表中,同时图片也转化成base64保存到数据库表中. PS:为了方便不同水平的开发人员阅读 ...
- Java解析JSON文件的方法
http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...
- Java解析xml文件遇到特殊符号&会出现异常的解决方案
文/朱季谦 在一次Java解析xml文件的开发过程中,使用SAX解析时,出现了这样一个异常信息: Error on line 60 of document : 对实体 "xxx" ...
随机推荐
- Javascript中的原型、原型链(十)
一.原型 每当创建一个函数时,函数就会包含一个prototype属性,这个属性其实相当于一个指针,指向调用该构造函数创建的对象原型. 这个对象原型里面有一个constructor属性,这个属性又指向构 ...
- 峰Redis学习(1)Redis简介和安装
是从博客:http://blog.java1234.com/blog/articles/310.html参考过来的: 第一节:Redis 简介 为什么需要NoSQL,主要应对以下问题,传统关系型数据库 ...
- 学习笔记之Nginx
NGINX | High Performance Load Balancer, Web Server, & Reverse Proxy https://www.nginx.com/ flawl ...
- [转][xml]SQL转义
SQL语句包含">"和"<"时,在PL/SQL中可以正常运行,但是放在XML中,编译报错,这是因为在XML文档中的所有文本都会被解析器解析,文本内容 ...
- Linux 网络命令找不到
1.安装好系统,命令找不到 如ifconfig等 解决办法: sudo apt-get install net-tools sudo ifconfig 如果命令前不想加sudo 在 .bashrc 文 ...
- 知识点:linux数据库备份
服务端启用二进制日志 如果日志没有启开,必须启用binlog,要重启mysql,首先,关闭mysql,打开/etc/my.cnf,加入以下几行: [mysqld] log-bin 然后重新启动mysq ...
- KPPW2.5 漏洞利用--SQL注入
KPPW2.5 漏洞利用--SQL注入 SQL注入--布尔型盲注 环境搭建 1,集成环境简单方便,如wamp,phpstudy.... 2,KPPW v2.2源码一份(文末有分享)放到WWW目录下面 ...
- mybaits插入时的一些总结
我们时长在批量插入时,需要获取插入数据的id. 这样: <insert id="insertUser" parameterType="gys.entity.User ...
- (转)C#实现注册码
原文地址:http://www.cnblogs.com/netcorner/archive/2011/08/31/2911922.html 开发软件时,当用到商业用途时,注册码与激活码就显得很重要了. ...
- c helloworld
#include <stdio.h> int main() { int i; printf("%s","hello, world"); } 1.#i ...