将控制台输入的资料存到文档中,并按照存入资料的年龄进行排序

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是否存在,不存在就创建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;

}}}

转载于:https://www.cnblogs.com/y-zz/p/7657237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值