【发布时间】:2018-04-15 12:19:20
【问题描述】:
基本上我添加了“import java.util.Scanner”。但我希望我的代码在没有那个库的情况下也能工作,并且只有 "import java.io*" 。但是我希望我的文本文件中的所有单词(字典中的英语单词,在这种情况下总共有 109562 个单词)都在字符串数组中。因此,在这种情况下,没有扫描仪。怎么做?
import java.io.*;
import java.util.Scanner;
public class tester{
public static void main (String [] args) throws IOException{
File f = new File("C:/Users/alienware14/Documents/words.txt");
String [] words = new String [109562];
readWords(f , words);
/*
System.out.println("----ALL WORDS IN WORDS.TXT----");
for(int i=0; i<words.length; i++){
System.out.println("");
System.out.print(words[i]);
} */
}
public static String [] readWords(File f , String [] words) throws FileNotFoundException {
Scanner s;
s = new Scanner(f);
for(int i = 0; i < words.length; i++){
while (words[i] == null) {
words[i] = s.next();
}
}
s.close();
return words;
}
}
【问题讨论】:
-
你试过什么?您是否在 Google 上搜索过“Java IO 教程”并阅读了一些内容?