⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vocabularylist.java

📁 将一篇文章中的单词提取出来
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Hashtable;

public class VocabularyList {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Hashtable<String, Integer> shtb = new Hashtable<String, Integer>();
		String inPath = "C:\\in.txt";
		String outPath = "C:\\out.txt";
		String[] keyArray = null;

		inputText(inPath, shtb);
		keyArray = shtb.keySet().toArray(new String[0]);
		keyArraySort(keyArray, shtb);
		outputTextWord(outPath, keyArray, shtb);
	}

	private static void inputText(String inPath, Hashtable<String, Integer> shtb) {

		BufferedReader bfr = null;
		String wordLine = "";
		String[] wordArray = null;
		int arraySize = 0;

		try {
			
			bfr = new BufferedReader(new FileReader(inPath));

			while ((wordLine = bfr.readLine()) != null) {
				
				wordArray = wordLine.split(" ");
				arraySize = wordArray.length;
				
				for (int i = 0; i < arraySize; i++) {
					
					if (!wordArray[i].equals("") && !wordArray[i].equals(" ")) {
						if (shtb.containsKey(wordArray[i])) {
							
							shtb.put(wordArray[i], shtb.get(wordArray[i]) + 1);
						} else {
							
							shtb.put(wordArray[i], 1);
						}
					}
				}
			}
			
			bfr.close();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
			try {
				
				bfr.close();
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}
		}

		return;
	}

	private static void keyArraySort(String[] keyArray,
			Hashtable<String, Integer> shtb) {

		int size = keyArray.length;
		String temp = null;
		int i, j;

		for (i = 0; i < size; i++) {
			for (j = 0; j < size - 1 - i; j++) {
				
				if (shtb.get(keyArray[j]) < shtb.get(keyArray[j + 1])) {
					
					temp = keyArray[j + 1];
					keyArray[j + 1] = keyArray[j];
					keyArray[j] = temp;
					
				} else if (shtb.get(keyArray[j]) == shtb.get(keyArray[j + 1])) {
					
					if (keyArray[j].compareTo(keyArray[j + 1]) > 0) {
						
						temp = keyArray[j + 1];
						keyArray[j + 1] = keyArray[j];
						keyArray[j] = temp;
					}
				}
			}
		}

		return;
	}

	private static void outputTextWord(String outPath, String[] keyArray,
			Hashtable<String, Integer> shtb) {

		BufferedWriter bfw = null;
		String str;
		String lineSeparator = System.getProperty("line.separator");

		try {
			
			bfw = new BufferedWriter(new FileWriter(outPath));

			int count = keyArray.length;
			
			for (int i = 0; i < count; i++) {
				
				str = keyArray[i];
				int temp = keyArray[i].length();
				for (int j = 0; j < 20 - temp; j++) {

					str += " ";
				}
				str += shtb.get(keyArray[i]);
				
				bfw.write(str);
				bfw.write(lineSeparator);
			}
			
			bfw.close();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
			try {
				
				bfw.close();
			} catch (Exception e1) {
				
				e1.printStackTrace();
			}
		}

		return;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -