将控制台输入的资料存到文档中,并按照存入资料的年龄进行排序
Posted 闫真真
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将控制台输入的资料存到文档中,并按照存入资料的年龄进行排序相关的知识,希望对你有一定的参考价值。
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;
}}}
以上是关于将控制台输入的资料存到文档中,并按照存入资料的年龄进行排序的主要内容,如果未能解决你的问题,请参考以下文章
用java写用户在控制台按照“yyyy/mm/dd”的格式输入出生日期,请计算用户的年龄