简体   繁体   English

如何从 java 中的 txt 文件中获取特定值?

[英]How can I get a specific value from txt file in java?

I have two.txt files:我有两个.txt 文件:

  1. mothers.txt (IdNum, name, age) for example: 6, Emily, 34 mothers.txt (IdNum, name, age) 例如:6, Emily, 34
  2. children.txt (num, sex(boy/girl), name, dateOfBirth, weight, height, IdnumOfMother) for example: 1 b Jackson 1999-10-15 3450 55 6 children.txt (num, sex(boy/girl), name, dateOfBirth, weight, height, IdnumOfMother) 例如:1 b Jackson 1999-10-15 3450 55 6

The only thing I can do is to write them all by String[].我唯一能做的就是把它们全部写成 String[]。

String child = "children.txt";
        BufferedReader reader = null;
        String line = "";

        reader = new BufferedReader(new FileReader(child));
        while ((line = reader.readLine()) != null){

            String[] row = line.split(",");

            for (String x : row){
                System.out.printf("%-10s", x );
            }
            System.out.println();
        }
        reader.close();

I have to find the tallest boy, the heaviest girl, the day when most children were born我必须找到最高的男孩,最重的女孩,大多数孩子出生的那一天

Can you help me?你能帮助我吗? It's my first time with.txt files.这是我第一次使用.txt 文件。 Thank you in advance.先感谢您。

Your problem requires searching files according to different conditions to get the desired attribute values.您的问题需要根据不同的条件搜索文件以获得所需的属性值。 Using Java to code the task is narrowly applicable.使用 Java 对任务进行编码是狭义适用的。

It is convenient to get this done using SPL, Java's open-source package. SPL only needs a few lines of code:使用Java的开源SPL package可以很方便的完成这个。SPL只需要几行代码:

| |A|
|:-|:-|
|1|=file("children.txt").import@ct()|
|2|=A1.select(sex=="b").maxp@a(height).(dateOfBirth)|
|3|=A1.select(sex=="g").maxp@a(weight).(dateOfBirth)|
|4|return A2,A3|

SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as spicify.splx and invoke it in Java as you call a stored procedure: SPL 提供 JDBC 驱动,由 Java 调用。只需将上述 SPL 脚本存储为 spicify.splx 并在调用存储过程时在 Java 中调用它:

…

Class.forName("com.esproc.jdbc.InternalDriver");

con = DriverManager.getConnection("jdbc:esproc:local://");

st = con.prepareCall("call spicify()");

st.execute();

…

You can also use SPL to conveniently associate two files by foreign key.您还可以使用 SPL 通过外键方便地关联两个文件。 To find the age of the tallest baby boy's mother and the age of the heaviest baby girl's mother, for example, SPL has the following code:例如,要查找最高的男婴母亲的年龄和最重的女婴母亲的年龄,SPL 有以下代码:

| |A|
|:-|:-|
|1|=file("mothers.txt").import@ct()|
|2|=file("children.txt").import@ct().switch(IdnumOfMother,A1:IdNum)|
|3|=A2.select(sex=="b").maxp@a(height).(IdnumOfMother.age)|
|4|=A2.select(sex=="g").maxp@a(weight).(IdnumOfMother.age)|
|5|return A3,A4|

View SPL source code .查看SPL 源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM