日期:2014-05-20 浏览次数:20792 次
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; public class Test { BufferedReader br = null; BufferedWriter bw = null; static String [][] a = null; public Test() throws IOException{ br = new BufferedReader(new FileReader(new File("c:"+File.separator+"exp.txt"))); bw = new BufferedWriter(new FileWriter(new File("f:"+File.separator+"1.txt"))); } public void sort(){ String tmp[]; for(int i = 1;i < a.length;i++){ for(int j = 0;j < a.length - i;j++ ){ if(Integer.parseInt(a[j][0]) > Integer.parseInt(a[j+1][0])){ tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } public void setArray() throws IOException{ String tmp = ""; for(int r = 0;r < a.length;r++){ for(int l = 0;l < a[r].length;l++){ if(l != a[r].length - 1){ tmp = tmp + a[r][l] + ","; }else{ tmp = tmp + a[r][l] + "\r\n"; } } bw.write(tmp); tmp = ""; } bw.close(); } public String[][] getArray() throws IOException{ String tmp[][] = new String[1024][1024]; int num = 0; while(br.read() != -1){ tmp[num] = br.readLine().split(","); num++; } tmp = Arrays.copyOf(tmp, num); br.close(); return tmp; } public static void main(String[] args) throws IOException { Test test = new Test(); a = test.getArray(); test.sort(); test.setArray(); } }
------解决方案--------------------
用Map试试
key为顺序值
value为一行值
------解决方案--------------------
如果学号等长,而且在行首,对整行排序即可。
// jdk7 List<String> conents = Files.readAllLine(path, Charset.defaultCharset()); Collections.sort(contents); Files.write(path, contents, Charset.defaultCharset());