-
已知文件 source.txt 中的内容如下:
username=root, password= 1234, id=1, level= 10
username=adimin, mima= 1234 , id=2, level= 11
yonghu= xiaoming ,dengji= 12 ,password= 1234,id= 3
yonghu= xiaofang ,dengji= 11 ,password= 1235,id= 3
其中,username、yonghu 都表示用户名,password、mima都表示密码,level、dengji都表示等级
- 在桌面上的这个source.txt文件拷贝到项目 file\data.txt 中(注意:方式不限,可以是移动或拷贝!)
- 读取文本中的数据,将每一行的数据封装成模型,存入 List<User> 中
- 去除集合中id重复的数据,只保留重复id的第一个数据
- 计算这些用户的平均等级、最高等级、最低等级
public class Morning {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(new FileReader("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\source.txt"));
ArrayList<User> users = new ArrayList<>();
while (scanner.hasNext()){
users.add(new User(scanner.nextLine()));
}
for (int i = 0; i < users.size()-1; i++) {
for (int j = i+1; j < users.size(); j++) {
if(users.get(i).getId().equals(users.get(j).getId())){
users.remove(j);
j--;
}
}
}
users.sort(Comparator.comparing(User::getLevel));
System.out.println("最低的等级是"+users.get(0)+"\n最高的等级是"+users.get(users.size()-1));
int num = 0;
for (User user : users) {
num+=user.getLevel();
}
System.out.println("平均等级为"+num/users.size());
System.out.println(users);
}
}
class User{
private String username;
private String password;
private String id;
private int level;
public String getUsername() {
return username;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public User() {
}
public User(String username, String password, String id,int level) {
this.username = username;
this.password = password;
this.id = id;
this.level = level;
}
public User(String message){
String[] split = message.split(",|=");
for (int i=0;i<split.length;i++) {
String s = split[i].trim();
if(s.matches("username|yonghu")){
this.username = split[i+1].trim();
}else if(s.matches("level|dengji")){
this.level = Integer.parseInt(split[i+1].trim());
}else if(s.matches("password|mima")){
this.password = split[i+1].trim();
}else if(s.matches("id")){
this.id = split[i+1].trim();
}
}
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", id='" + id + '\'' +
", level=" + level +
'}';
}
}
-
//老师从控制台录入学员信息,每行数据代表一个学员信息
//具体输入格式是:
//第一次输入
//name:zhangsan;age:34;sex:Male
//第二次
//age:28;name:lisi;sex:FeMale
//第三次
//sex:Male;name:wangwu;age:31
要求:
1.将读取的数据放入List<Person>
2.将List进行序列化到工程下的file/source.txt,并反序列化测试
3.找到姓名是zhangsan的人,并将所有数据以key-value形式存入file/zhang.properties文件
4.读取内容,将年龄修改成40并再次写入.properties文件.
public class Morning {
static class Person implements Serializable{
private int age;
private String name;
private String sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
public Person(int age, String name, String sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
}
public static List<Person> getPerson(){
Scanner scanner = new Scanner(System.in);
ArrayList<Person> people = new ArrayList<>();
for (int j = 0; j < 3; j++) {
String next = scanner.next();
String[] split = next.split(":|;");
Person person = new Person();
for (int i = 0; i < split.length; i++) {
if(split[i].equals("name")){
person.name = split[++i];
}else if(split[i].equals("age")){
person.age = Integer.parseInt(split[++i]);
}else if(split[i].equals("sex")){
person.sex = split[++i];
}
}
people.add(person);
}
return people;
}
public static void main(String[] args) {
List<Person> persons = getPerson();
System.out.println(persons);
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:/D/bigData/code/demo1/bigdataDay19/src/com/qf/person.txt"));){
for (int i = 0; i < persons.size(); i++) {
out.writeObject(persons.get(i));
}
out.writeObject(null);
}catch (Exception e){
e.printStackTrace();
}
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:/D/bigData/code/demo1/bigdataDay19/src/com/qf/person.txt"));
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\D\\bigData\\code\\demo1\\bigdataDay19\\src\\com\\qf\\zhang.properties"))
){
Person p = (Person)in.readObject();
ArrayList<Person> people = new ArrayList<>();
while (p!=null){
people.add(p);
p = (Person)in.readObject();
}
System.out.println("size "+people.size());
for (Person person : people) {
if(person.getName().equals("zhangsan")){
out.writeObject(person);
}
}
out.writeObject(null);
}catch (Exception e){
e.printStackTrace();
}
Person zhangsan = null;
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:/D/bigData/code/demo1/bigdataDay19/src/com/qf/zhang.properties"));
){
zhangsan= (Person)in.readObject();
System.out.println("张三"+zhangsan);
zhangsan.setAge(40);
System.out.println("zhangsan"+zhangsan);
}catch (Exception e){
e.printStackTrace();
}
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\D\\bigData\\code\\demo1\\bigdataDay19\\src\\com\\qf\\zhang.properties"));){
out.writeObject(zhangsan);
out.writeObject(null);
}catch (Exception e){
e.printStackTrace();
}
}
}
-
记录⼀个程序的使⽤次数,超过5次提示去注册
public class Work3 {
public static void main(String[] args) throws Exception{
File file = new File("work3.properties");
if(!file.exists()){
file.createNewFile();
}
Properties properties = new Properties();
properties.load(new FileReader(file));
String time = properties.getProperty("time");
int count = 1;
if(time!=null){
int i = Integer.parseInt(time);
count = ++i;
}
properties.setProperty("time",""+count);
if(count>5){
System.out.println("请注册后使用");
}
properties.store(new FileOutputStream("Work3.properties"),"这是啥");
}
}
-
有五个学⽣,每个学⽣有3⻔课的成绩, 从键盘输⼊以上数据(包括姓名,三⻔课 成绩), 输⼊的格式:如:zhagnsan,30,40,60计算出总成绩, 并把学⽣的信 息和计算出的总分数⾼低顺序存放在磁盘⽂件"stud.txt"中。
public class Work4 {
static class Student{
String name;
int ch;
int math;
int eng;
int sum;
public void initStudent(String message){
String[] split = message.split(",|,");
name = split[0].trim();
ch = Integer.parseInt(split[1]);
math = Integer.parseInt(split[2]);
eng = Integer.parseInt(split[3]);
sum = ch+math+eng;
}
public int getSum() {
return sum;
}
@Override
public String toString() {
return "" +
"name='" + name +
", ch=" + ch +
", math=" + math +
", eng=" + eng +
", sum=" + sum +
'\n';
}
}
public static void main(String[] args) {
TreeSet<Student> students = new TreeSet<Student>(Comparator.comparing(Student::getSum).reversed());
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
Student student = new Student();
student.initStudent(scanner.nextLine());
students.add(student);
}
try(BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\D\\bigData\\code\\demo1\\bigdataDay19\\src\\com\\qf\\stud.txt"))){
for (Student student : students) {
out.write(student.toString().getBytes());
}
out.flush();
}catch (Exception e){
e.printStackTrace();
}
System.out.println(students);
}
}
-
设计⼀个⽅法,计算⼀个⽂件夹的总⼤⼩(由所有的⼦⽂件、⼦⽂件夹中 的⼦⽂件夹⼤⼩组成
public class Work5 {
public static void main(String[] args) {
Long calculate = calculate(new File("C:\\D\\bigData\\code\\demo1\\bigdataDay19\\src"));
System.out.println(calculate);
}
public static Long calculate(File file){
Long len = 0L;
if(file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
len+= calculate(file1);
}
return len;
}
else return file.length();
}
}
-
在桌⾯上创建⼀个30层的⽂件夹
public class Work6 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("C:\\Users\\User\\Desktop");
for (int i = 0; i < 30; i++) {
stringBuffer.append("\\"+i);
File file = new File(stringBuffer.toString());
file.mkdir();
}
}
}
-
获取桌⾯上所有的⾮隐藏的⽂件(不要⽂件夹 )
public class Work7 {
public static void main(String[] args) {
File file = new File("C:\\Users\\User\\Desktop");
File[] files = file.listFiles(f -> !f.isHidden() && f.isFile());
for (File file1 : files) {
System.out.println(file1.getName());
}
}
}
-
递归删除⼀个⾮空的⽂件夹
public class Work8 {
public static void main(String[] args) {
File file = new File("C:\\Users\\User\\Desktop\\a");
delDir(file);
}
public static void delDir(File file){
if(!file.exists())
return;
if(file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
delDir(file1);
}
}
file.delete();
}
}
-
列举⼀个⽂件夹中的所有的⽂件,及⼦⽂件夹中的⼦⽂件
public class Work9 {
public static void main(String[] args) {
File file = new File("C:\\Users\\User");
getFile(file);
}
public static void getFile(File file){
if(file==null)
return;
if(file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
getFile(file1);
}
}else System.out.println(file.getName());
}
}
-
设计⼀个⽅法,实现⼀个⽂件夹的拷⻉(包括⽂件夹中的⼦⽂件和⼦⽂件 夹)
public class Work10 {
public static void main(String[] args) {
File file = new File("E:\\a");
StringBuffer stringBuffer = new StringBuffer("E:\\b");
copyAll(file,stringBuffer);
}
public static void copyAll(File start,StringBuffer target){
if(!start.exists())
return;
if(start.isDirectory()){
target.append("\\"+start.getName());
new File(target.toString()).mkdir();
File[] files = start.listFiles();
for (File file : files) {
copyAll(file,target);
}
}else copyFile(start,target);
}
public static void copyFile(File start,StringBuffer stringBuffer){
try(BufferedReader reader = new BufferedReader(new FileReader(start));
BufferedWriter writer = new BufferedWriter(new FileWriter(stringBuffer.toString()+start.getName()))
){
String s = reader.readLine();
writer.write(s);
}catch (Exception e){
e.printStackTrace();
}
}
}