【作业】3月8日

时间:2021-02-16 11:48:19

 1、对文件pet.template中的肉容进行替换,再输出到文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package  com.java.work;
import  java.io.BufferedReader;
import  java.io.BufferedWriter;
import  java.io.FileNotFoundException;
import  java.io.FileReader;
import  java.io.FileWriter;
import  java.io.IOException;
 
public  abstract  class  WorkPet {
     public  static  void  main(String[] args) {
         FileReader fr =  null ;
         BufferedReader br =  null ;
         
         FileWriter fw =  null ;
         BufferedWriter bw =  null ;
         try  {
             //读取pet.template文件中的内容
             fr =  new  FileReader( "pet.template" );
             br =  new  BufferedReader(fr);
             String str =  "" ;
             String s =  new  String();
             String news =  new  String();
             while ((str = br.readLine())!= null ) {
                 System.out.println(str);
                 //将读取出的来内容拼接到字符串s中
                 s = s + str;
             }
             //替换s中的内容
             //替换前的内容
             System.out.println(s);
             news = s.replace( "{name}" "1" ).replace( "{type}" "2" ).replace( "{master}" "3" );
             //替换后的内容
             System.out.println(news);
             //将替换后的内容再次输出到pet.template文件中
             fw =  new  FileWriter( "pet.template" true );
             bw =  new  BufferedWriter(fw);
             bw.newLine();
             bw.write(news);
             bw.flush();
         catch  (FileNotFoundException e) {
             e.printStackTrace();
         catch  (IOException e) {
             e.printStackTrace();
         } finally  {
             try  {
                 if (bw!= null ) {
                     bw.close();
                 }
                 if (fw!= null ) {
                     fw.close();
                 }
                 if (br!= null ) {
                     br.close();
                 }
                 if (fr!= null ) {
                     fr.close();
                 }
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
         
     }
}


【作业】3月8日替换前文本内空:

控制台输出:

【作业】3月8日

替换后:

【作业】3月8日

2、 用序列化和反序列化  写一个可以多次注册,并且可以用注册过的所有用户登录的功能

  要求每次注册一个用户,当再次注册用户时,不能把之前注册过的用户覆盖掉
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package  com.java.worktwo;
 
import  java.io.Serializable;
/**
  * 用户类
  * @author Administrator
  *
  */
public  class  User  implements  Serializable{
     private  String name;
     private  String password;
     private  Long phoneNumber;
     
     public  User() {}
     
     public  User(String name, String password, Long phoneNumber) {
         this .name = name;
         this .password = password;
         this .phoneNumber = phoneNumber;
     }
     
     public  String getName() {
         return  name;
     }
     public  void  setName(String name) {
         this .name = name;
     }
     public  String getPassword() {
         return  password;
     }
     public  void  setPassword(String password) {
         this .password = password;
     }
     public  Long getPhoneNumber() {
         return  phoneNumber;
     }
     public  void  setPhoneNumber(Long phoneNumber) {
         this .phoneNumber = phoneNumber;
     }
 
     public  String toString() {
         return  "User [name="  + name +  ", password="  + password +  ", phoneNumber="  + phoneNumber +  "]" ;
     }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package  com.java.worktwo;
 
import  java.io.FileInputStream;
import  java.io.FileNotFoundException;
import  java.io.IOException;
import  java.io.ObjectInputStream;
import  java.util.ArrayList;
import  java.util.List;
import  java.util.Scanner;
 
/**
  * 用户登录
  * @author Administrator
  *
  */
public  class  UserLogin {
     static  Scanner cxj =  new  Scanner(System.in);
     public  static  void  main(String[] args) {
         List<User> list =  new  ArrayList<User>();
         FileInputStream fis =  null ;
         ObjectInputStream ois =  null ;
 
         try  {
             fis =  new  FileInputStream( "user.txt" );
             if ((fis.available())!= 0 ) {
                 ois =  new  ObjectInputStream(fis);
                 list = (ArrayList<User>)ois.readObject();
             } else  {
                 System.out.println( "当前无帐号信息!" );
                 System.out.println( "请先注册!" );
                 return ;
             }
         catch  (FileNotFoundException e1) {
             e1.printStackTrace();
         catch  (IOException e) {
             e.printStackTrace();
         catch  (ClassNotFoundException e) {
             e.printStackTrace();
         } finally  {
             try  {
                 if (ois!= null ) {
                     ois.close();
                 }
                 if (fis!= null ) {
                     fis.close();
                 }
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
             
         System.out.print( "请输入用户名:" );
         String name = cxj.next();
         System.out.print( "请输入密码:" );
         String password = cxj.next();
 
         int  count =  0 ;
         for (User u : list) {
             if (name.equals(u.getName())&&(password.equals(u.getPassword()))) {
                 System.out.println( "登录成功!" );
                 break ;
             } else  {
                 count++;
             }
         }
         if (count>=list.size()){
             System.out.println( "登录失败!" );
         }
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package  com.java.worktwo;
 
import  java.io.FileInputStream;
import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.ObjectInputStream;
import  java.io.ObjectOutputStream;
import  java.util.ArrayList;
import  java.util.List;
import  java.util.Scanner;
 
/**
  * 用户注册
  * @author Administrator
  *
  */
public  class  UserRegister {
     static  Scanner cxj =  new  Scanner(System.in);
     public  static  void  main(String[] args) {
         List<User> list =  new  ArrayList<User>();
 
         FileInputStream fis =  null ;
         ObjectInputStream ois =  null ;
 
         try  {
             fis =  new  FileInputStream( "user.txt" );
             if ((fis.available())!= 0 ) {
                 ois =  new  ObjectInputStream(fis);
                 list = (ArrayList<User>)ois.readObject();
             } else  {
                 System.out.println( "当前无帐号信息!" );
             }
             
         catch  (FileNotFoundException e1) {
             e1.printStackTrace();
         catch  (IOException e) {
             e.printStackTrace();
         catch  (ClassNotFoundException e) {
             e.printStackTrace();
         } finally  {
             try  {
                 if (ois!= null ) {
                     ois.close();
                 }
                 if (fis!= null ) {
                     fis.close();
                 }
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
 
         FileOutputStream fos =  null ;
         ObjectOutputStream oos =  null ;
 
         System.out.print( "请输入用户名:" );
         String name = cxj.next();
         System.out.print( "请输入密码:" );
         String password = cxj.next();
         System.out.print( "请输入手机号码:" );
         Long phoneNumber = cxj.nextLong();
 
         User user =  new  User(name,password,phoneNumber);
         list.add(user);
 
         try  {
             fos =  new  FileOutputStream( "user.txt" );
             oos =  new  ObjectOutputStream(fos);
             oos.writeObject(list);
             oos.flush();
             oos.close();
             System.out.println( "注册成功!" );
         catch  (FileNotFoundException e) {
             e.printStackTrace();
         catch  (IOException e) {
             e.printStackTrace();
         } finally  {
             try  {
                 if (oos!= null ) {
                     oos.close();
                 }
                 if (fos!= null ) {
                     fos.close();
                 }
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
     }
}

用户几号信息记录文本:

【作业】3月8日

结果示例:当前无帐号信息无法登录

【作业】3月8日

注册帐号:

【作业】3月8日

【作业】3月8日