简体   繁体   English

如何对文本文件中的记录进行排序以及如何使用 Java 打印其他 txt 文件?

[英]How do I sort records in a text file and how to print other txt file using Java?

Some amendments to the data in the text file.对文本文件中的数据进行了一些修改。 I have tried the suggested code but I'm not successfully writing it again in the txt file with this format.我已经尝试过建议的代码,但我没有成功地用这种格式在 txt 文件中再次写入它。 I've tried the collection.我试过收藏。 sort but it writes the data in long lines.排序,但它以长行写入数据。

My txt file contains these data:我的 txt 文件包含这些数据:

PID1,POND^AMY,F,19890224 PID2,WILLIAMS^RORY,M,19881102 PID3,POND^AMY,F,19890224 PID4,POND^AMY,F,20010911 PID1,池^AMY,F,19890224 PID2,WILLIAMS^RORY,M,19881102 PID3,池^AMY,F,19890224 PID4,池^AMY,F,20010911

How can I sort those data in ascending order and store them again in a txt file after sorting?如何按升序对这些数据进行排序,并在排序后将它们再次存储在 txt 文件中?

0: PID1,POND^AMY,F,19890224 PID3,POND^AMY,F,19890224 PID4,POND^AMY,F,20010911 1: PID2,WILLIAMS^RORY,M,19881102 0: PID1,池^AMY,F,19890224 PID3,池^AMY,F,19890224 PID4,池^AMY,F,20010911 1: PID2,WILLIAMS^RORY,M,19881102

You can easily sort records in a text file and export the result to another txt file with esProc SPL (a java based open-source scripting language for data processing):您可以轻松地对文本文件中的记录进行排序,并使用集算器 SPL (一种基于 java 的数据处理开源脚本语言)将结果导出到另一个 txt 文件:

IIUC,the data.txt like this: IIUC,data.txt 是这样的:

PID1,POND^AMY,F,19890224 PID2,WILLIAMS^RORY,M,19881102 PID3,POND^AMY,F,19890224 PID4,POND^AMY,F,20010911

and you wanna sort records by the 2nd field(POND^AMY,WILLIAMS^RORY) and the result.txt like this:并且您想按第二个字段(POND^AMY,WILLIAMS^RORY)和 result.txt 对记录进行排序,如下所示:

0: PID1,POND^AMY,F,19890224 PID3,POND^AMY,F,19890224 PID4,POND^AMY,F,20010911 1: PID2,WILLIAMS^RORY,M,19881102

SPL code:声压级代码:

=file("result.txt").write(file("data.txt").read().split(" ").group(~.split@c()(2)).((#-1)/": "/~.concat(" ")).concat(" "))

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

...
Class.forName("com.esproc.jdbc.InternalDriver");
con = DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call sort.splx()");
st.execute();
...

with

<!-- https://mvnrepository.com/artifact/com.scudata.esproc/esproc -->
<dependency>
    <groupId>com.scudata.esproc</groupId>
    <artifactId>esproc</artifactId>
    <version>20220402</version>
</dependency>

You can also execute the SPL code within a Java program as we execute a SQL statement:当我们执行 SQL 语句时,您还可以在 Java 程序中执行 SPL 代码:

...
st = con.prepareStatement("==file(\"result.txt\").export@c(file(\"data.txt\").import@c().sort(_2))");
st.execute();
...

Or if your txt actually is a csv format like this:或者,如果您的 txt 实际上是这样的 csv 格式:

PID1,POND^AMY,F,19890224
PID2,WILLIAMS^RORY,M,19881102
PID3,POND^AMY,F,19890224
PID4,POND^AMY,F,20010911

and you wanna get the result.txt like this:你想得到这样的 result.txt :

PID1,POND^AMY,F,19890224
PID3,POND^AMY,F,19890224
PID4,POND^AMY,F,20010911
PID2,WILLIAMS^RORY,M,19881102

use this SPL code:使用这个 SPL 代码:

=file("result.txt").export@c(file("data.txt").import@c().sort(_2))

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

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