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