一 关于文本文件的复制及内容的反转问题
.给了一个文本文件是GBK编码。你把这个文件中的4句,复制到另外一个文本文件,这个文件的编码是UTF-8,而且四句诗要反转。
public class MyTest1 {
public static void main(String[] args) throws IOException, IOException {
BufferedReader a1 = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\诗.txt"),"gbk"));
PrintWriter w1 = new PrintWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\诗2.txt"),"utf-8"));
List<String> list = new ArrayList<>();
while(true) {
String line = a1.readLine();
if(line == null) {
break;
}
list.add(line);
}
Collections.reverse(list);
for (String line : list) {
w1.println(line);
}
w1.close();
a1.close();
}
}
二 关于在文本文件里统计取排名取特定的要求的问题
给你一个文本文件 需要你完成如下要求
1 武力值排名前三的值是多少
2 求武力值最高3人名字
3 .统计各个地区的武将
4 .统计各个年龄段的武将 比如:1019岁,2029岁,30~39岁 40~49岁 50~59岁 60岁以上
5 .统计各个年龄段的武将男女比例
.6 统计平均年龄
7 统计平均年龄最高的地区
import java.io.*;
import java.util.*;
public class MyTest1 {
public static void main(String[] args) throws IOException {
List<Person> list = question1();
System.out.println("=========================== 武力前三");
question2(list);
System.out.println("=========================== 武力排名前三");
question3(list);
System.out.println("=========================== 统计各个地区的武将");
Map<String, List<String>> locationMap = question4(list);
System.out.println(locationMap.get("平原"));
System.out.println("=========================== 男女比例");
Map<String, List<Person>> sexMap = question5(list);
System.out.println(sexMap.get("男").size() +":" +sexMap.get("女").size());
System.out.println("=========================== 统计各个年龄段的武将");
Map<String, List<String>> ageMap = question6(list);
for (Map.Entry<String, List<String>> entry : ageMap.entrySet()) {
System.out.println(entry.getKey());
System.out.println("\t"+entry.getValue());
}
System.out.println("=========================== 统计各个年龄段的武将男女比例");
Map<String,Map<String,List<String>>> ageMap2 = question7(list);
for (Map.Entry<String, Map<String, List<String>>> entry : ageMap2.entrySet()) {
System.out.print(entry.getKey());
System.out.println("\t"+entry.getValue().get("男").size()+":"+entry.getValue().get("女").size());
}
System.out.println("=========================== 统计平均年龄");
System.out.println(avgAge(list));
System.out.println("=========================== 统计平均年龄最高的地区");
System.out.println(locationAvgAge(list));
}
private static String locationAvgAge(List<Person> list) {
Map<String,List<Person>> locationMap = new HashMap<>();
for (Person p : list) {
List<Person> people = locationMap.get(p.getLocation());
if(people==null) {
people = new ArrayList<>();
locationMap.put(p.getLocation(), people);
}
people.add(p);
}
TreeMap<Double,String> map = new TreeMap<>((a,b)->b.compareTo(a));
for (Map.Entry<String, List<Person>> entry : locationMap.entrySet()) {
List<Person> people = entry.getValue();
double totalAge = 0;
for (Person p : people) {
totalAge += p.getAge();
}
map.put(totalAge/ people.size(),entry.getKey());
}
return map.firstEntry().getValue();
}
private static double avgAge(List<Person> list) {
double totalAge = 0;
for (Person p : list) {
totalAge += p.getAge();
}
return totalAge/list.size();
}
private static Map<String,Map<String,List<String>>> question7(List<Person> list) {
Map<String,Map<String,List<String>>> map = new TreeMap<>();
for (int i = 10; i < 70; i+=10) {
Map<String,List<String>> sexMap = new HashMap<>();
sexMap.put("男", new ArrayList<>());
sexMap.put("女", new ArrayList<>());
map.put(ageRange(i), sexMap);
}
for (Person p : list) {
List<String> people = map.get(ageRange(p.getAge())).get(p.getSex());
if(people != null) {
people.add(p.getName());
}
}
return map;
}
private static Map<String, List<String>> question6(List<Person> list) {
Map<String,List<String>> map = new TreeMap<>();
for (int i = 10; i < 70; i+=10) {
map.put(ageRange(i), new ArrayList<>());
}
for (Person p : list) {
map.get(ageRange(p.getAge())).add(p.getName());
}
return map;
}
private static String ageRange(int age) {
switch (age / 10) {
case 1:
return "10~19岁";
case 2:
return "20~29岁";
case 3:
return "30~39岁";
case 4:
return "40~49岁";
case 5:
return "50~59岁";
default:
return "60岁以上";
}
}
private static Map<String, List<Person>> question5(List<Person> list) {
Map<String,List<Person>> map = new HashMap<>();
map.put("男", new ArrayList<>());
map.put("女", new ArrayList<>());
for (Person p : list) {
map.get(p.getSex()).add(p);
}
return map;
}
private static Map<String, List<String>> question4(List<Person> list) {
// 问题4
Map<String,List<String>> map = new HashMap<>();
for (Person p : list) {
List<String> people = map.get(p.getLocation());
if(people==null) {
people = new ArrayList<>();
map.put(p.getLocation(), people);
}
people.add(p.getName());
}
return map;
}
private static void question3(List<Person> list) {
// 问题3
// 先把所有武力值放入set,倒序,并去重
TreeSet<Integer> set = new TreeSet<>((a, b)-> b-a);
for(Person p: list){
set.add(p.getForces());
}
// 取前三武力
Set<Integer> top3 = new HashSet<>();
for (int i = 0; i < 3; i++) {
top3.add(set.pollFirst());
}
for(Person p : list) {
if(top3.contains(p.getForces())){
System.out.println(p.getName());
}
}
}
private static void question2(List<Person> list) {
// 问题2, 求武力最高3人 比较器的lambda写法教给了学员
Collections.sort(list, (p1, p2)-> p2.getForces()-p1.getForces());
for (int i = 0; i < 3; i++) {
System.out.println(list.get(i).getName());
}
}
public static List<Person> question1() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\output.txt")));
ArrayList<Person> list = new ArrayList<>();
while(true) {
String line = reader.readLine();
if(line == null) {
break;
}
String[] split = line.split("\t");
Person p = new Person();
p.setId(Integer.parseInt(split[0]));
p.setName(split[1]);
p.setLocation(split[2]);
p.setSex(split[3]);
p.setBirth(Integer.parseInt(split[4]));
p.setDeath(Integer.parseInt(split[5]));
p.setForces(Integer.parseInt(split[6]));
list.add(p);
}
reader.close();
return list;
}
}
class Person {
private int id;
private String name;
private String location;
private String sex;
private int death;
private int birth;
private int forces;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return death-birth;
}
public int getDeath() {
return death;
}
public void setDeath(int death) {
this.death = death;
}
public int getBirth() {
return birth;
}
public void setBirth(int birth) {
this.birth = birth;
}
public int getForces() {
return forces;
}
public void setForces(int forces) {
this.forces = forces;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", location='" + location + '\'' +
", sex='" + sex + '\'' +
", age=" + (death-birth) +
", forces=" + forces +
'}';
}
}