importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileReader;importjava.io.FileWriter;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.HashSet;importjava.util.Iterator;importjava.util.Set;publicclassSortStringList {publicstaticvoidmain(String[] args) {
String content=openFile("D:\\test.txt");//分词String[] tokens=content.split("\\s");
ArrayListlist=newArrayList();for(inti=0; i
}//去重,用简单的set不能保持原来顺序,如果想保持原来顺序可以利用LinkedHashSetSetset=newHashSet(list);
String[] result=newString[set.size()];
Iteratorite=set.iterator();
String temp="";for(inti=0; ite.hasNext(); i++) {
result[i]=(String) ite.next();
p(result[i]);
}//排序Arrays.sort(result);for(inti=0; i
temp+=result[i]+"\t";
}
p(temp);
writeFile(temp,"D:\\test_sort.txt");
}publicstaticString openFile(String szFileName) {try{
BufferedReader bis=newBufferedReader(newFileReader(szFileName));
String szContent="";
String szTemp;while((szTemp=bis.readLine())!=null) {
szContent+=szTemp+"\n";
}
bis.close();returnszContent;
}catch(Exception e) {
e.printStackTrace();return"";
}
}publicstaticvoidwriteFile(String content, String szFileName) {try{
BufferedWriter bis=newBufferedWriter(newFileWriter(szFileName));
bis.write(content);
bis.flush();
bis.close();
}catch(Exception e) {
e.printStackTrace();
}
}privatestaticvoidp(String str) {try{
System.out.println(str);
}catch(Exception e) {
e.printStackTrace();
}
}
}