首页 > 代码库 > 22.2-按照升序显示不重复的单词

22.2-按照升序显示不重复的单词

编写一个程序--从文本文件当中读取单词,并将所有不重复的单词按照升序显示,文本文件被作为命令行参数传递。

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.Set;import java.util.TreeSet;public class exercise22_2 {	public static void main(String args[]) throws IOException  {		String fileName = args[0];		Set<String> treeSet = new exercise22_2().getWordDiffereceByAsec(fileName);		System.out.println(treeSet);	}    	/**	 * 如果采用read那样显示的话,第一个字母是不会显示的	 * @param fileName	 * @return	 * @throws IOException	 */	private Set<String> getWordDiffereceByAsec(String fileName)			throws IOException {		BufferedReader buffer = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));		Set<String> wordSet = new TreeSet<String>();//		while(buffer.read() != -1){//			String word = buffer.readLine();//			wordSet.add(word);//		}		String word;		while((word = buffer.readLine())!= null){			wordSet.add(word);		}		return wordSet;	}}

  

22.2-按照升序显示不重复的单词