对input.txt文件中的学号进行冒泡排序,从小到大,文件是以逗号分隔的文件如
3110005837,张三,,计算机科学与技术10(1),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
3110005838,李四,,计算机科学与技术10(1),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(大概有200行这样的数据)
排好后输出到output.txt
崩溃,做了好久都做不出,求打救
3110005837,张三,,计算机科学与技术10(1),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
3110005838,李四,,计算机科学与技术10(1),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(大概有200行这样的数据)
排好后输出到output.txt
崩溃,做了好久都做不出,求打救
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();
}
}
key为顺序值
value为一行值
List<String> conents = Files.readAllLine(path, Charset.defaultCharset());
Collections.sort(contents);
Files.write(path, contents, Charset.defaultCharset());