一个discs.txt中的文本内容如下
Test_Iscicles Circle-Square-Triangle Ska NoLabel 1995 2 9,12
Elvis_Presley Jailhouse_Rock Rock RCA 1991 17 72,23
Massive_Attack Protection
Massive_Attack Blue_Lines
要求是设计一个程序先从txt中读取数据到array,然后运用冒泡排序法将演唱者(如果演唱者相同则按照专辑名称)按照字母顺序a到z排序,最后再将结果输出为一个新的txt文件,要求格式和第一个music的一样,结果类似于:
Elvis_Presley Jailhouse_Rock Rock RCA 1991 17 72,23
Massive_Attack Blue_Lines
Massive_Attack Protection
Test_Iscicles Circle-Square-Triangle Ska NoLabel 1995 2 9,12
在论坛查阅相关信息,运用BufferedReader已经将TXT数据读取,代码如下:
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\discs.txt")));
String line1 = bf.readLine();
String[] CD1 = line1.split(" ") ;
String line2 = bf.readLine();
String[] CD2 = line2.split(" ");
String line3 = bf.readLine();
String[] CD3 = line3.split(" ") ;
String line4 = bf.readLine();
String[] CD4 = line4.split(" ") ;
但是到了排序这一阶段却不知道怎么写了,小弟JAVA新手,还请各位大侠指点~~
Test_Iscicles Circle-Square-Triangle Ska NoLabel 1995 2 9,12
Elvis_Presley Jailhouse_Rock Rock RCA 1991 17 72,23
Massive_Attack Protection
Massive_Attack Blue_Lines
要求是设计一个程序先从txt中读取数据到array,然后运用冒泡排序法将演唱者(如果演唱者相同则按照专辑名称)按照字母顺序a到z排序,最后再将结果输出为一个新的txt文件,要求格式和第一个music的一样,结果类似于:
Elvis_Presley Jailhouse_Rock Rock RCA 1991 17 72,23
Massive_Attack Blue_Lines
Massive_Attack Protection
Test_Iscicles Circle-Square-Triangle Ska NoLabel 1995 2 9,12
在论坛查阅相关信息,运用BufferedReader已经将TXT数据读取,代码如下:
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\discs.txt")));
String line1 = bf.readLine();
String[] CD1 = line1.split(" ") ;
String line2 = bf.readLine();
String[] CD2 = line2.split(" ");
String line3 = bf.readLine();
String[] CD3 = line3.split(" ") ;
String line4 = bf.readLine();
String[] CD4 = line4.split(" ") ;
但是到了排序这一阶段却不知道怎么写了,小弟JAVA新手,还请各位大侠指点~~
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author monitor
* Created on 2011-1-6, 23:30:52
*/
public class NameSort { public NameSort(){
} /**
* 从文件中获得名字列表
* @param file 存名字的文件
* @return 名字列表
* @throws FileNotFoundException
* @throws IOException
*/
public static List<String> getNameList(String file) throws FileNotFoundException, IOException{
List<String> nameList=new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(file));
String name=null;
while((name=br.readLine())!=null){
nameList.add(name);
System.out.println(name);
}
if(br!=null)br.close();
return nameList;
} /**
* 将名字写到指定文件
* @param nameList 名字列表
* @param file 指定文件
* @throws IOException 文件操作异常
*/
public static void exportNameToFile(List<String>nameList,String file) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for(String name:nameList){
bw.write(name);
bw.newLine();
}
bw.flush();
bw.close();
} public static void main(String[] args){
try {
List<String> nameList = getNameList("test.txt");
//排序
Collections.sort(nameList);
exportNameToFile(nameList,"name.txt");
} catch (FileNotFoundException ex) {
Logger.getLogger(NameSort.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NameSort.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
平时我们在数据结构里学排序算法的时候比较的都是整数,这个谁都知道怎么比较;但是两个对象怎么比较大小呢,关键就在compareTo()方法了
import java.util.Collections;
import java.util.List;class Name implements Comparable{
private String name;
private String spell; public Name(String name,String spell){
this.name=name;
this.spell=spell;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSpell() {
return spell;
} public void setSpell(String spell) {
this.spell = spell;
} public int compareTo(Object o) {
if(o instanceof Name){
Name n=(Name)o;
return spell.compareTo(n.getSpell());
}else{
return 1;
}
}
}
public class Test {
public static void main(String[] args) {
List<Name> nameList=new ArrayList<Name>();
nameList.add(new Name("邓永枢","dengys"));
nameList.add(new Name("陈济民","chenjm"));
nameList.add(new Name("方镇云","fangzy"));
nameList.add(new Name("刘彪","liub"));
nameList.add(new Name("叶攀","yep"));
nameList.add(new Name("劳高津","laogj"));
nameList.add(new Name("黄品","huangp"));
nameList.add(new Name("黄高栋","huanghgd"));
nameList.add(new Name("温生文","wensm"));
//...... Collections.sort(nameList);
for(Name name:nameList){
System.out.println(name.getName());
}
}
}