📌  相关文章
📜  从文本文件中读取单词并按字母升序显示所有单词的程序 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:36.255000             🧑  作者: Mango

代码示例2
import java.util.*;
import java.io.*;


public class Task1 {
    public static void main(String[] args) throws Exception {
        // Check number of arguments passed

        if (args.length != 1) {
            System.out.println("Usage: TextFile");
            System.exit(0);

        }
String filename = args[0];

TreeSettreeSet = new TreeSet();
try
{
    Scanner in = new Scanner(new File(filename));
    String line;
    while ((line = in.nextLine())!=null)
    {
        String[] tokens = line.split("[||n|\t|\r|.|,|)|(|-|\"]");



        for (int i = 0; i  < tokens.length; i++)
        //for (int i = tokens.length -1; i >= 0; i--)
            treeSet.add(tokens[i]);

    }
}
catch (Exception ex)
{

}

Iterator iterator = treeSet.iterator();

System.out.println("\nDisplay words in ascending order");

while (iterator.hasNext()) {
System.out.println(iterator.next());

}
}
}