这是我想要读入我的程序的文本文件的屏幕截图。
我想构建一个GUI来显示特定用户的属性。所以我需要从这个文本文件中提取信息,但我不知道究竟是怎么回事。完美的结果是将每个标头作为String名称,然后将该列中的其余元素存储到该String。任何帮助将不胜感激。
FileReader fr = new FileReader("charList.txt");
BufferedReader bf = new BufferedReader(fr);
String line = "";
while (bf.readLine()!= null){
line += bf.readLine();
}
String [] temp = new String [];
temp = line.split("\\s");
答案 0 :(得分:0)
'标题'行是一个问题,将它们连贯地合并将是一个挑战。我建议你只是忽略那个和硬代码(或在配置文件中)的问题:
private class CharListField {
private String fieldname;
private int offset;
private int len;
}
CharListField[] fields = {new CharListField("user id", 0, 8),
.....,
new CharListField("end xp", 40, 6)};
BufferedReader bf = new BufferedReader(fr);
String line = "";
boolean gotheaders = false;
while ((line = bf.readLine()) != null){
if (!gotheaders) {
// skip lines till you get an empty one.
if (line.trim().length() == 0) {
gotheaders = true;
}
continue;
}
for (CharListField field : fields) {
String value = line.substring(field.offset, field.offset + field.len).trim();
// do something with the value for the field....
}
}
答案 1 :(得分:0)
您应该首先知道您的文件,然后您会找到阅读它的方法。
可能是在您的文件中使用了分隔数据结构的分隔符。
但更可能的是,您的文件是按位置构建的。
这可能意味着可以在0到10之间找到user is
,starting xp
可以在11到20之间找到。
答案 2 :(得分:-1)
您是否考虑将文本文件结构化为json?