將控制臺輸入的資料存到文檔中,並按照存入資料的年齡進行排序
package LX10_11;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class WenJian {
public static void main(String[] args) throws IOException {
List<KeHu> list = new ArrayList<KeHu>();//創建一個集合,存放文件中截取到的客戶資料
//關聯文件
File file1=new File("E:\\客戶資料.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file1));
//判斷file1
try {
if(file1.createNewFile()) {
System.out.println("[成功創建]");
}else {
System.out.println( "[文件存在-不覆蓋創建]");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//從控制臺給文件中寫內容
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String line=br1.readLine();
while(!line.equals("cl")) {
bw.write(line);
line=br1.readLine();
}
//關閉
br1.close();
bw.flush();
bw.close();
//用集合實現排序
//jihe(list, file1);
ShuZu(file1);
}
private static void ShuZu(File file1) throws FileNotFoundException, IOException {
KeHu str[] = new KeHu[4];//創建數組存放客戶對象
BufferedReader br = new BufferedReader(new FileReader(file1));
String s1 = br.readLine();
String[] t1 = s1.split(";");
String[] t2 = new String[2];
int a=0;
for(String i:t1) {
//System.out.println("i的值---"+i);
t2=i.split("、");
String name = t2[0];
int age = Integer.parseInt(t2[1]);
KeHu kh = new KeHu(name,age);
str[a]=kh;
a++;
}
System.out.println("用數組排序前----------:");
for(KeHu h:str) {
System.out.println(h);
}
//對存入對象的年齡按從小到大的順序排列,冒泡法
for(int i=0;i<str.length-1;i++) {
for(int j=0;j<str.length-i-1;j++) {
KeHu t;
if(str[j].getAge()>str[j+1].getAge()) {
t = str[j];
str[j] = str[j+1];
str[j+1] = t;
}
}
}
System.out.println("用數組排序後------:");
for(KeHu h:str) {
System.out.println(h);
}
}
private static void jihe(List<KeHu> list, File file1) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(file1));
String s1 = br.readLine();
String[] t1 = s1.split(";");
String[] t2 = new String[2];
for(String i:t1) {
//System.out.println("i的值---"+i);
t2=i.split(",");
String name = t2[0];
int age = Integer.parseInt(t2[1]);
KeHu kh = new KeHu(name,age);
list.add(kh);
//System.out.println(kh);
}
System.out.println("用集合排序前:\n"+list);
Collections.sort(list);//對集合中元素按年齡進行排序
System.out.println("用集合排序後:\n"+list);
}
}
package LX10_11;
public class KeHu implements Comparable<KeHu>{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public KeHu() {
super();
// TODO Auto-generated constructor stub
}
public KeHu(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "名字=" + name + ", 年齡" + age ;
}
@Override
public int compareTo(KeHu o) {
if(this.age>o.getAge()) {
return -1;
}else if(this.age>o.getAge()) {
return 0;
}else {
return 1;
}}}
將控制臺輸入的資料存到文檔中,並按照存入資料的年齡進行排序