对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("imput.txt")));
bw = new BufferedWriter(new FileWriter(new File("output.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].trim()) > Integer.parseInt(a[j+1][0].trim())){
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;
String str;
while((str = br.readLine()) != null){
tmp[num] = str.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();
}
}
package test19;import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;public class IOTset { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String[] sourceStudentData=new String[]{};
sourceStudentData=readFile(sourceStudentData);
sourceStudentData=sort(sourceStudentData);
write(sourceStudentData);
} private static void write(String[] sourceStudentData) throws IOException{
PrintWriter pw=new PrintWriter(new FileOutputStream("SortStudentDate.txt"));
for(String data : sourceStudentData){
pw.println(data);
}
pw.close();
} private static String[] readFile(String[] sourceStudentData) throws IOException{
BufferedReader br=new BufferedReader(new FileReader("StudentData.txt"));
String str=null;
while((str=br.readLine())!=null){
if(str.equals("")){
continue;
}
sourceStudentData=Arrays.copyOf(sourceStudentData, sourceStudentData.length+1);
sourceStudentData[sourceStudentData.length-1]=str;
}
br.close();
return sourceStudentData;
} private static String[] sort(String[] studentData) {
for (int i = 0; i < studentData.length-1; i++) {
for (int j = 0; j < studentData.length-i-1; j++) {
long a=Long.parseLong(studentData[j].split(",")[0]);
long b=Long.parseLong(studentData[j+1].split(",")[0]);
String temp="";
if(a>b){
temp=studentData[j];
studentData[j]=studentData[j+1];
studentData[j+1]=temp;
}
}
}
return studentData;
}}