所以我可以在我的文本文件中搜索一个字符串,但是,我想在这个 ArrayList 中对数据进行排序并实现一个算法。是否可以从文本文件中读取并将文本文件中的值 [Strings] 存储在 String[] 数组中。
也可以分开字符串吗?所以不是我的数组有:
[Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]
是否可以将数组设置为:
["Alice", "was" "beginning" "to" "get"...]
.
public static void main(String[]args) throws IOException
{
Scanner scan = new Scanner(System.in);
String stringSearch = scan.nextLine();
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
List<String> words = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
for(String sLine : words)
{
if (sLine.contains(stringSearch))
{
int index = words.indexOf(sLine);
System.out.println("Got a match at line " + index);
}
}
//Collections.sort(words);
//for (String str: words)
// System.out.println(str);
int size = words.size();
System.out.println("There are " + size + " Lines of text in this text file.");
reader.close();
System.out.println(words);
}