读取的文件内容如下
test_5 test_3 test_2 test_10
读取代码如下
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
* webkfa.com
* @author 学到老
*
*/
public class Test {
public static void main(String[] args) {
BufferedReader is=null;
try {
is = new BufferedReader(new FileReader("c:/1.txt"));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
int i=0;
String s="";
ArrayList list=new ArrayList();
while(null != (s=is.readLine())){
list.add(s.split("_")[1]);
}
//从文本里面读出来的顺序
System.out.println("排序前:");
int[] a = new int[list.size()];
for (int j = 0; j < list.size(); j++) {
a[j]=Integer.parseInt(list.get(j).toString());
System.out.println(list.get(j));
}
Arrays.sort(a);
System.out.println("排序后:");
for (int j = 0; j < a.length; j++) {
System.out.println(a[j]);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
运行结果
排序前:
5
3
2
10
排序后:
2
3
5
10