一个txt文件中包含:ID、姓氏、名字、父名、地址、余额、城市通话时间、长途通话时间。编写一个Java静态方法读取文件中的信息,并将所有条目,按余额数量排序。只做一个表格
时间: 2024-01-26 10:02:12 浏览: 159
以下是一个简单的Java静态方法,用于从txt文件中读取信息并按余额数量排序:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortByBalance {
public static void main(String[] args) {
List<Person> persons = readFromFile("data.txt");
sortByBalance(persons);
System.out.println("ID\t姓氏\t名字\t父名\t地址\t余额\t城市通话时间\t长途通话时间");
for (Person person : persons) {
System.out.println(person.getId() + "\t" + person.getLastName() + "\t" + person.getFirstName() + "\t" + person.getFatherName() +
"\t" + person.getAddress() + "\t" + person.getBalance() + "\t" + person.getCityCallTime() + "\t" + person.getLongDistanceCallTime());
}
}
public static void sortByBalance(List<Person> persons) {
Collections.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
if (p1.getBalance() > p2.getBalance()) {
return 1;
} else if (p1.getBalance() < p2.getBalance()) {
return -1;
} else {
return 0;
}
}
});
}
public static List<Person> readFromFile(String fileName) {
List<Person> persons = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
int id = Integer.parseInt(values[0]);
String lastName = values[1];
String firstName = values[2];
String fatherName = values[3];
String address = values[4];
double balance = Double.parseDouble(values[5]);
double cityCallTime = Double.parseDouble(values[6]);
double longDistanceCallTime = Double.parseDouble(values[7]);
Person person = new Person(id, lastName, firstName, fatherName, address, balance, cityCallTime, longDistanceCallTime);
persons.add(person);
}
} catch (IOException e) {
e.printStackTrace();
}
return persons;
}
private static class Person {
private int id;
private String lastName;
private String firstName;
private String fatherName;
private String address;
private double balance;
private double cityCallTime;
private double longDistanceCallTime;
public Person(int id, String lastName, String firstName, String fatherName, String address, double balance, double cityCallTime, double longDistanceCallTime) {
this.id = id;
this.lastName = lastName;
this.firstName = firstName;
this.fatherName = fatherName;
this.address = address;
this.balance = balance;
this.cityCallTime = cityCallTime;
this.longDistanceCallTime = longDistanceCallTime;
}
public int getId() {
return id;
}
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String getFatherName() {
return fatherName;
}
public String getAddress() {
return address;
}
public double getBalance() {
return balance;
}
public double getCityCallTime() {
return cityCallTime;
}
public double getLongDistanceCallTime() {
return longDistanceCallTime;
}
}
}
```
这个程序假设数据文件的格式如下:
```
ID 姓氏 名字 父名 地址 余额 城市通话时间 长途通话时间
1 张 三 李四 北京 100.0 20.5 30.0
2 李 四 王五 上海 50.0 10.0 20.0
3 王 五 赵六 广州 200.0 15.0 25.0
```
程序将数据文件读取到一个Person列表中,然后按余额数量对列表进行排序。最后,程序将列表输出到控制台。
阅读全文