我有一个包含单词列表的文本文件,我需要使用Java按字母顺序对其进行排序。我认为我需要做到这一点,而不是在java中存储在集合或数组中,但你可以创建临时文件。
FileReader r1 = new FileReader("C:/Users/394286/Desktop/file11.txt");
FileReader r2 = new FileReader("C:/Users/394286/Desktop/file21.txt");
BufferedReader f1 = new BufferedReader(r1);
BufferedReader f2 = new BufferedReader(r2);
String s = null;
s = f1.readLine();
String s1 = f2.readLine();
Set<String> l1 = new TreeSet<String>();
Set<String> l2 = new TreeSet<String>();
Set<String> l3 = new TreeSet<String>();
Set<String> l4 = new TreeSet<String>();
Set<String> same = new TreeSet<String>();
Set<String> diff1 = new TreeSet<String>();
Set<String> diff2 = new TreeSet<String>();
if (s1 == null && s == null){
System.out.println("Both the Files are Empty");
} else if (s1 == null && s != null) {
System.out.println(" File 2 is Empty");
} else if (s1 != null && s == null) {
System.out.println("File 1 is Empty");
}
while (s != null){
l1.add(s);
s = f1.readLine();
}
while (s1 != null){
l2.add(s1);
s1 = f2.readLine();
}
this is my try
从文件中读取单词并存储在普通数组中,例如String[] words
。现在你可以使用定义的排序算法对这个数组进行排序。
辅助边缘排序帮助选择排序
试试这个:
String[] strArr = // get all the Strings from the file and save it in array
Arrays.sort(strArr);// Sorts the Array