从文本文件中拆分单词并排序

【问题】

I have a text file rext.txt and I'm trying to get first 100 random words from each line of the text file and put them into String array, but it's not working. I figured how to separate files from text and put them into array, but I can't figure where to include 100 to get them sorted. Thank you!!!

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Random;

import java.util.Scanner;

 

public class New2

    {

     public static void main(String[] args) throws FileNotFoundException

        {

         Scanner sc = new Scanner(new File("dictionary.txt"));

         while (sc.hasNext())  

             {

             String word = sc.next();

             sc.nextLine();  

             String[] wordArray = word.split(" ");

             //System.out.println(Arrays.toString(wordArray));

             int idx = new Random().nextInt(wordArray.length);

             String random = (wordArray[idx]);

             System.out.println(random);

        }

}

}

【回答】

按行取数据,将每行拆分为单词,取前100个,再打乱顺序排列。用JAVA硬写很麻烦。如无特殊要求,用SPL实现再集成进Java会比较简单,脚本如下:


A

1

=file("d:\\source.txt").read@n()

2

=A1.(~.words().to(100).sort(rand()))

A1:按行读取文本

A2:将每行拆分为单词,取前100个,再打乱顺序

写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本