java 解析 txt 固定格式 类似json 这明显不不能解析,只能readline一行一行读取 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加了20分,这个格式肯定可以解析的,因为这个是配置文件即使自己写工具类 能给个demo么- - txt文件你只能一行行把它读出来.... 读出来之后, 这个json格式的数据,你当然可以把它转换成Json啊, 直接从string转换成Json对象就OK了, 推荐jackson.. import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.TreeMap;import org.junit.Test;/** * @author hipo */public class StringTest { public static List<Map<String, String>> parseStringToList() { File file = new File("E:/error.txt"); BufferedReader reader = null; List<Map<String, String>> list = new ArrayList<Map<String,String>>(); try { reader = new BufferedReader(new FileReader(file)); String id = null; int count = 0; // 花括号数,{加1, }减1 String tempString = null; StringBuilder used_by_heroes = new StringBuilder(); Map<String, String> map = new TreeMap<String, String>(); while ((tempString = reader.readLine()) != null) { tempString = tempString.replace("\"", "").trim(); if (tempString.matches("\\d+")) { id = tempString; map.put("id", id); } else if (tempString.equals("{")) { count++; } else if (tempString.equals("}")) { count--; if (count == 0) { map.put("used_by_heroes", used_by_heroes.toString()); list.add(map); map = new TreeMap<String, String>(); used_by_heroes = new StringBuilder(); } } else if (tempString.matches("\\S+\\s+\\S+")) { if (count == 1) { String[] s = tempString.split("\\s+"); map.put(s[0], s[1]); } else if (count == 2) { if (used_by_heroes.length() != 0) { used_by_heroes.append("#**#"); } used_by_heroes.append(tempString); } } } } catch (Exception e) { e.printStackTrace(); } return list; } @Test public void test() { parseStringToList(); }}其中used_by_heroes属性我相信你这应该有很多条,我把这些都拼成一个字符串了,中间用"#**#"隔开的,这个属性需要你后期自己去解析。这一条数据应该是对应一个实体类,你可以直接把它映射到实体类中,返回List<Object> java nextLine()能否接受并输出接受的汉字 急~ Java文本框问题? 计算机英语原版和中文版的疑问 java中如何刷新JTree的节点信息?? 用utf-8编码(encode)unicode码时不会有信息损失,但禁止用utf-8解码非utf-8编码的字节流。总之Utf-8可以编码任何unicode 码,但只能解码 一个关于快捷键的问题~ 如何设置建立连接数据库? java里有messagebox吗? jive安装,连接数据库问题 一道面试题, 谁能帮我看看怎么做, 实在想不出来了 double丢失精度问题,大侠们帮帮忙~ jpa 关系映射 如何才能不建立外键关系
这个格式肯定可以解析的,因为这个是配置文件
即使自己写工具类 能给个demo么- -
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;import org.junit.Test;
/**
* @author hipo
*/
public class StringTest { public static List<Map<String, String>> parseStringToList() {
File file = new File("E:/error.txt");
BufferedReader reader = null;
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
try {
reader = new BufferedReader(new FileReader(file));
String id = null;
int count = 0; // 花括号数,{加1, }减1
String tempString = null;
StringBuilder used_by_heroes = new StringBuilder();
Map<String, String> map = new TreeMap<String, String>();
while ((tempString = reader.readLine()) != null) {
tempString = tempString.replace("\"", "").trim();
if (tempString.matches("\\d+")) {
id = tempString;
map.put("id", id);
} else if (tempString.equals("{")) {
count++;
} else if (tempString.equals("}")) {
count--;
if (count == 0) {
map.put("used_by_heroes", used_by_heroes.toString());
list.add(map);
map = new TreeMap<String, String>();
used_by_heroes = new StringBuilder();
}
} else if (tempString.matches("\\S+\\s+\\S+")) {
if (count == 1) {
String[] s = tempString.split("\\s+");
map.put(s[0], s[1]);
} else if (count == 2) {
if (used_by_heroes.length() != 0) {
used_by_heroes.append("#**#");
}
used_by_heroes.append(tempString);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Test
public void test() {
parseStringToList();
}
}
其中used_by_heroes属性我相信你这应该有很多条,我把这些都拼成一个字符串了,中间用"#**#"隔开的,这个属性需要你后期自己去解析。
这一条数据应该是对应一个实体类,你可以直接把它映射到实体类中,返回List<Object>