java - 如何从java中的文本文件中提取列

标签 java file-io text-files

这是我想要读入程序的文本文件的屏幕截图。

我想构建一个 GUI 来显示特定用户的属性。所以我需要从这个文本文件中提取信息,但我不知 Prop 体如何提取。完美的结果是将每个标题作为字符串名称,然后将该列中的其余元素存储到该字符串中。任何帮助将不胜感激。

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");

enter image description here

最佳答案

“标题”行是一个问题,将它们连贯地合并起来将是一个挑战。我建议您忽略该问题并通过硬编码(或在配置文件中)解决问题:

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....
    }
}

关于java - 如何从java中的文本文件中提取列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19801447/

相关文章:

java - 相机预览很好,但前置相机产生的照片非常暗

Python:相互依赖的进程/线程队列

reactjs - 如何在 native react 中打开文本文件并从中读取内容?

c++ - C++ 中的文件 I/O 代码

Java 泛型 : How to create a to-n collection with configurable value holder type

java - Java中创建对象的方法

java - AsyncTask onProgressUpdate(双...值)

c - 读取一行直到 C 中的某个字符

类似于 Unix 排序的算法

c# - 为什么这不能从我的文本文件中读取整数?系统格式异常