我是初学者,我正在尝试用 Java 读取 .tsv 数据并将行保存到 ArrayList 中。我为它编写了一个方法,但我得到的唯一的东西就是行 ID,仅此而已......我找不到错误。请你帮助我好吗?
public static ArrayList<String[]> tsvr(File test2) throws IOException {
BufferedReader TSVReader = new BufferedReader(new FileReader(test2));
String line = TSVReader.readLine();
ArrayList<String[]> Data = new ArrayList<>(); //initializing a new ArrayList out of String[]'s
try {
while (line != null) {
String[] lineItems = line.split("\n"); //splitting the line and adding its items in String[]
Data.add(lineItems); //adding the splitted line array to the ArrayList
line = TSVReader.readLine();
} TSVReader.close();
} catch (Exception e) {
System.out.println("Something went wrong"); …Run Code Online (Sandbox Code Playgroud) 我有一个制表符分隔的文件,如下所示:
A 1234
A 123245
A 4546
A 1234
B 24234
B 4545
C 1234
C 1234
Output:
A 3
B 2
C 1
Run Code Online (Sandbox Code Playgroud)
基本上,我需要属于第一列的唯一值的计数,所有这些都在带有管道的一个突击队中。正如您所看到的,可能会有一些重复项,例如“A 1234”。我对 awk 或 cut 有一些想法,但似乎都不起作用。他们只是打印出所有唯一对,而我需要考虑第一列中的值来计算第二列中的唯一值。
awk -F " "'{print $1}' file.tsv | uniq -c
cut -d' ' -f1,2 file.tsv | sort | uniq -ci
Run Code Online (Sandbox Code Playgroud)
我非常感谢你的帮助!先感谢您。