0
我想從我的java目錄收集一個文件,收集文檔中的所有單詞,將所有單詞放入TreeSet
,然後打印出整個單詞TreeSet
。當我嘗試節目,所有這些從控制檯TreeSet
打印出是TreeSet沒有打印任何東西
Input file:
trees.docx
[]
它只是這些空brackets.Note結束:裏面的trees.docx文件只有一行字「樹木之類的東西。」這裏是我的代碼:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class CountWords {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("Input file: ");
String fileName = sc.next();
File inputFile = new File(fileName);
Scanner in = new Scanner(inputFile);
Set<String> words = new TreeSet<String>();
// only happens if there is a next string
while(in.hasNext()){
words.add(in.next()); //adds this string to the treeSet initialized above
}
System.out.println(words); // prints the treeSet
}
}
Java不能真正讀取docx文件爲純文本... –
如果您想要閱讀Microsoft,您需要使用['Apache POI'](https://poi.apache.org/)文件 –
非常感謝你!我在我的電腦上用.txt文件嘗試了這個功能,並且工作完美無瑕。 – Neffero