How can I get a specific value from txt file in java

 

问题

https://stackoverflow.com/questions/70226212/how-can-i-get-a-specific-value-from-txt-file-in-java

I have two .txt files:

1.     mothers.txt (IdNum, name, age) for example : 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

The only thing I can do is to write them all by 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. Thank you in advance.

解答

这个问题需要按不同的条件查找文件获取特征值。直接JAVA 写起来不通用。

Java 下的开源包 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提供了JDBC 供 JAVA 调用,把上面的脚本存为 spicify.splx,在 JAVA 中以存储过程的方式调用脚本文件:

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

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

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

st.execute();

也可以很方便地把两个文件按外健进行关联,例如需要找到最高的男婴和最重的女婴母亲的年龄:


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

SPL源代码:https://github.com/SPLWare/esProc

问答搜集