鱼C论坛

 找回密码
 立即注册
查看: 1799|回复: 2

[已解决]Java如何反序列出(读取文本中所有内容)

[复制链接]
发表于 2022-9-26 15:16:36 | 显示全部楼层 |阅读模式
30鱼币
  1. 主类
  2. import java.io.*;
  3. import java.nio.charset.StandardCharsets;
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6. import java.util.Scanner;

  7. public class StudentSystem implements Serializable{
  8.     static  ArrayList<User> Ulist = new ArrayList<>();
  9.     static ArrayList<Student> Slist = new ArrayList<>();
  10.     static  User user = new User();
  11.     static  Student student = new Student();
  12.     static String path1,path2;
  13.     static {
  14.         path2 = "studentsystem\\student.txt";
  15.         path1 = "studentsystem\\user.txt";
  16.     }
  17. //    static File file1 = new File(path1),file2 = new File(path2);
  18. //    static {
  19. //        if (!file1.exists()) {
  20. //            try {
  21. //                file1.createNewFile();
  22. //            } catch (IOException e) {
  23. //                e.printStackTrace();
  24. //            }
  25. //            if (!file1.exists()) {
  26. //                try {
  27. //                    file1.createNewFile();
  28. //                } catch (IOException e) {
  29. //                    e.printStackTrace();
  30. //                }
  31. //            }
  32. //        }
  33. //    }
  34.     public static void main(String[] args) throws Exception {
  35.         readFile(Ulist,null,path1,user,null);
  36.         enterUserSystem(Ulist);
  37.     }

  38.     private static void enterUserSystem(ArrayList<User> list) throws Exception{
  39.         User user = new User();
  40.         System.out.println("---------------欢迎来到Java版学生管理系统---------------");
  41.         while (true){
  42.             System.out.println("--------请选择1.登录 2.注册 3.忘记密码--------");
  43.             Scanner sc = new Scanner(System.in);
  44.             String choice = sc.next();
  45.             switch (choice) {
  46.                 case "1":
  47.                     logIn(Ulist);
  48.                     break;
  49.                 case "2":
  50.                     register(Ulist);
  51.                     writeFile(Ulist,path1);
  52.                     break;
  53.                 case "3":
  54.                     forgotPassword(Ulist);
  55.                     break;
  56.                 case "4":
  57.                     System.out.println("---------------退出用户界面---------------");
  58.                     System.exit(0);
  59.                 default:
  60.                     System.out.println("选项不存在!默认退出");
  61.                     System.exit(0);
  62.             }
  63.         }
  64.     }



  65.     private static void enterStudentSystem() throws Exception {
  66.         loop: while(true){
  67.             System.out.println("添加学生信息,请按1");
  68.             System.out.println("删除学生信息,请按2");
  69.             System.out.println("修改学生信息,请按3");
  70.             System.out.println("查询学生信息,请按4");
  71.             System.out.println("退出,请按5");
  72.             System.out.print("请输入您的选择:");
  73.             Scanner sc = new Scanner(System.in);
  74.             String choice = sc.next();
  75.             switch (choice) {
  76.                 case "1":
  77.                     readFile(null,Slist,path2,null,student);
  78.                     addStudent(Slist);
  79.                     writeFile(Slist,path2);
  80.                     break;
  81.                 case "2":
  82.                     readFile(null,Slist,path2,null,student);
  83.                     deleteStudent(Slist);
  84.                     break ;
  85.                 case "3":
  86.                     readFile(null,Slist,path2,null,student);
  87.                     changeStudent(Slist);
  88.                     writeFile(Slist,path2);
  89.                     break ;
  90.                 case "4":
  91.                     readFile(null,Slist,path2,null,student);
  92.                     queryStudent(Slist);
  93.                     break ;
  94.                 case "5":
  95.                     System.out.println("---------------退出学生管理系统---------------");
  96.                     break loop;
  97.                 default:
  98.                     System.out.println("选项不存在!请重新输入");
  99.             }
  100.         }
  101.     }
  102.     private static void register(ArrayList<User> list) throws IOException {
  103.         String s1,s2,s3,s4;
  104.         s1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXVZ";
  105.         s3 = "0123456789xX";
  106.         s2 = "0123456789";
  107.         s4 = "~`!@#$%^&*()_+-={}[]|\\;:'"<>,.?/";
  108.         User user = new User();
  109.         Scanner sc = new Scanner(System.in);
  110.         String userName,password,repassword,personId,phoneNum;
  111.         boolean flag1 = true,flag2 = true,flag3 = true,flag4 = true;
  112.        while(flag3) {
  113.            System.out.print("请输入用户名(长度必须在3到15位):");
  114.            userName = sc.next();
  115.            int l = userName.length();
  116.             if(l < 3||l > 15){
  117.                 System.out.println("输入用户名语法错误,请重新输入");
  118.             }
  119.            if(userContain(list,userName)) {
  120.                System.out.println("用户名已被注册,请再输入其他用户名!");
  121.                continue;
  122.            }else{
  123.                System.out.println("注册用户名成功");
  124.                user.setUserName(userName);
  125.                flag3 = false;
  126.            }
  127.         }
  128.         while(flag4){
  129.             System.out.print("请设置密码(长度必须在8到15位):");
  130.             password = sc.next();
  131.             int l = password.length();
  132.             if(l < 8||l > 15){
  133.                 System.out.println("设置密码语法错误,请重新输入");
  134.             }else {
  135.                 System.out.print("密码设置成功,请再次输入核对:");
  136.                while (flag4){
  137.                    repassword = sc.next();
  138.                    if(password.equals(repassword)){
  139.                        System.out.println("密码核对成功,已录入");
  140.                        repassword = null;
  141.                        user.setPassword(password);
  142.                        flag4 = false;
  143.                    }else {
  144.                        System.out.println("密码核对错误,请再次核对");
  145.                }
  146.                 }
  147.             }
  148.         }
  149.         while (flag2){
  150.             System.out.println("请输入身份证号码:");
  151.             personId = sc.next();
  152.             String id = personId;
  153.             if((id.charAt(0)=='0')||(id.length()!=18)){
  154.                 flag2 = true;
  155.             }else if (isContain(id.substring(0,17).toString(),s1)){
  156.                 flag2 = true;
  157.             }else if(!(isContain(id.substring(17),s3))){
  158.                 flag2 = true;
  159.             }else {
  160.                 flag2 = false;
  161.             }
  162.             if(flag2 == false){
  163.                 user.setPersonId(id);
  164.             }else {
  165.                 System.out.println("输入错误,请重新输入:");
  166.             }
  167.         }
  168.         while (flag1 ){
  169.             System.out.println("请输入手机号码:");
  170.             phoneNum = sc.next();
  171.             if((isContain(phoneNum,s1))||(isContain(phoneNum,s4))){
  172.                 flag1 = true;
  173.             }else if((phoneNum.length() != 11)||phoneNum.charAt(0) == '0'){
  174.                 flag1 = true;
  175.             }else {
  176.                 flag1 = false;
  177.             }
  178.             if(flag1 == true){
  179.                 System.out.println("输入格式错误!请重新输入:");
  180.             }else {
  181.                 System.out.println("输入成功");
  182.                 user.setPhoneNum(phoneNum);
  183.             }
  184.         }
  185.         if((flag2 == flag3 == flag1 == flag4)&&(flag2 == false)){
  186.            System.out.println("注册成功");
  187.             list.add(user);
  188.         }else {
  189.             System.out.println("注册失败");
  190.         }
  191.     }
  192.     private static void logIn(ArrayList<User> list) throws Exception {
  193.         Scanner sc = new Scanner(System.in);
  194.         System.out.print("请输入用户名:");
  195.         String name = sc.next();
  196.         while (true){
  197.             if(userContain(list,name)){
  198.                 break ;
  199.             }else {
  200.                 System.out.println("未找到该用户,请先注册");
  201.                 return;
  202.             }
  203.         }
  204.         System.out.print("请输入密码:");
  205.         String key = sc.next();
  206.        while (true){
  207.            String code = getCode();
  208.            System.out.println("验证码:" + code);
  209.            System.out.print("请输入验证码:");
  210.            String recode = sc.next();
  211.            if(code.equalsIgnoreCase(recode)){
  212.                System.out.println("验证码正确");
  213.                break;
  214.            }else{
  215.                System.out.println("验证码输入错误,请重新输入");
  216.            }
  217.        }
  218.         User user = new User(name,key,null,null);
  219.         if(isRight(list,user)){
  220.             System.out.println("登录成功,正在进入学生管理系统");
  221.             enterStudentSystem();
  222.         }else{
  223.             System.out.println("登录失败,账户名或密码输入错误");
  224.         }
  225.     }
  226.     private static void forgotPassword(ArrayList<User> list){
  227.         Scanner sc = new Scanner(System.in);
  228.         System.out.print("请输入用户名:");
  229.         String name = sc.next();
  230.         while (true){
  231.             if(userContain(list,name)){
  232.                 break ;
  233.             }else {
  234.                 System.out.println(userContain(list, name));
  235.                 System.out.println("未找到该用户,请先注册");
  236.                 return;
  237.             }
  238.         }
  239.         int index = userByName(list,name);
  240.         User user = new User();
  241.         user = list.get(index);
  242.         String pI, pN, key, rekey;
  243.        while (true){
  244.            System.out.print("请输入身份证号码:");
  245.            pI = sc.next();
  246.            System.out.print("请输入电话号码:");
  247.            pN = sc.next();
  248.            if (!(user.getPersonId().equalsIgnoreCase(pI) && user.getPhoneNum().equals(pN))) {
  249.                System.out.println("身份证或电话号码输入错误!!!");
  250.                continue;
  251.            }else {
  252.                break;
  253.            }
  254.        }
  255.       clock: while(true){
  256.             System.out.print("请设置密码(长度必须在8到15位):");
  257.             key = sc.next();
  258.             int l = key.length();
  259.             if(l < 8||l > 15){
  260.                 System.out.println("设置密码语法错误,请重新输入");
  261.             }else {
  262.                 System.out.print("密码设置成功,请再次输入核对:");
  263.                 while (true){
  264.                     rekey = sc.next();
  265.                     if(key.equals(rekey)){
  266.                         System.out.println("密码核对成功,已录入");
  267.                         rekey = null;
  268.                         break clock;
  269.                     }else {
  270.                         System.out.println("密码核对错误,请再次核对");
  271.                         continue ;
  272.                     }
  273.                 }
  274.             }
  275.         }
  276.        user.setPassword(key);
  277.     }
  278.     private static int userByName(ArrayList<User> list,String name) {
  279.         for (int i = 0; i < list.size(); i++) {
  280.             User user = new User();
  281.             user = list.get(i);
  282.             if(user.getUserName().equals(name)){
  283.                 return i;
  284.             }
  285.         }
  286.         return -1;
  287.     }
  288.     public static void addStudent(ArrayList<Student> list) throws IOException {
  289.         Student stu = new Student();
  290.         Scanner sc = new Scanner(System.in);
  291.         String id = null;
  292.         while (true) {
  293.             System.out.print("请输入学号:");
  294.             id = sc.next();
  295.             boolean key = idOnly(list, id);
  296.             if(key){
  297.                 System.out.println("您添加的学号已存在,请重新输入!!!");
  298.             }else{
  299.                 stu.setId(id);
  300.                 list.add(stu);
  301.                 break;
  302.             }
  303.         }
  304.         System.out.print("请输入姓名:");
  305.         String name = sc.next();
  306.         System.out.print("请输入籍贯:");
  307.         String address = sc.next();
  308.         System.out.print("请输入年龄:");
  309.         int age = sc.nextInt();
  310.          stu = new Student(name,id,address,age);
  311.         list.add(stu);
  312.         System.out.println("学生信息添加成功!");
  313.     }
  314.     public static void deleteStudent(ArrayList<Student> list){
  315.         Scanner sc = new Scanner(System.in);
  316.         System.out.print("请输入您的学号来删除对应信息:");
  317.         String id = sc.next();
  318.         int bool = getIndex(list,id);
  319.         if(bool >= 0){
  320.             list.remove(bool);
  321.             System.out.println("删除成功");
  322.         }else{
  323.             System.out.println("很抱歉,未能找到您的学号!");
  324.         }
  325.     }
  326.     public static void changeStudent(ArrayList<Student> list){
  327.             System.out.print("请输入您的学号以便于修改您对应的信息:");
  328.             Scanner sc = new Scanner(System.in);
  329.             String id = null;
  330.             while (true) {
  331.                  id = sc.next();
  332.                 boolean flag = idOnly(list, id);
  333.                 if(flag){
  334.                     break;
  335.                 }else{
  336.                     System.out.println("您输入的学号不存在!请重新输入!");
  337.                 }
  338.             }
  339.             int index = getIndex(list,id);
  340.           First: while(true) {
  341.                     System.out.println("修改学号请按1\n修改姓名请按2\n修改年龄请按3\n修改籍贯请按4\n其余均为放弃修改\n");
  342.                     System.out.print("请按照提示输入:");
  343.                     String choice = sc.next();
  344.                     switch (choice) {
  345.                         case "1":
  346.                             System.out.print("请输入您想修改学号为:");
  347.                             list.get(index).setId(sc.next());
  348.                             break;
  349.                         case "2":
  350.                             System.out.print("请输入您想修改姓名为:");
  351.                             list.get(index).setName(sc.next());
  352.                             break;
  353.                         case "3":
  354.                             System.out.print("请输入您想修改年龄为:");
  355.                             list.get(index).setAge(sc.nextInt());
  356.                             break;
  357.                         case "4":
  358.                             System.out.print("请输入您想修改籍贯为:");
  359.                             list.get(index).setAddress(sc.next());
  360.                             break;
  361.                         default:
  362.                             System.out.println("退出");
  363.                     }
  364.                     Second:while(true) {
  365.                       System.out.println("是否继续修改?(yes/no)");
  366.                       String chance = sc.next();
  367.                       if (chance.equalsIgnoreCase("no")) {
  368.                           break First;
  369.                       } else if (chance.equalsIgnoreCase("yes")) {
  370.                           System.out.println("继续修改");
  371.                           break Second;
  372.                       } else {
  373.                           System.out.println("输入错误请重新选择:");
  374.                       }
  375.               }
  376.             }
  377.     }
  378.     public static void queryStudent(ArrayList<Student> list){
  379.         if(list.size() == 0){
  380.             System.out.println("没有学生信息,请添加后再查询");
  381.             return;
  382.         }
  383.         System.out.println("学号\t\t\t" + "姓名\t" + "年龄\t" + "籍贯");
  384.         for (int i = 0; i < list.size(); i++) {
  385.             Student stu = new Student();
  386.             stu = list.get(i);
  387.             System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
  388.         }
  389.     }
  390.     public static boolean idOnly(ArrayList<Student> list,String id) {
  391.        int flag = getIndex(list,id);
  392.        if(flag >= 0){
  393.            return true;
  394.        }
  395.        return false;
  396.     }
  397.     public static int getIndex(ArrayList<Student> list,String id){
  398.         for (int i = 0; i < list.size(); i++) {
  399.             String sid = list.get(i).getId();
  400.             if (sid.equals(id)) {
  401.                 return i;
  402.             }
  403.         }
  404.         return -1;
  405.     }
  406.     public static boolean isContain(String s1,String s2){
  407.         for (int i = 0; i < s1.length(); i++) {
  408.             char c1 = s1.charAt(i);
  409.             for (int k = 0; k < s2.length(); k++) {
  410.                 char c2 = s2.charAt(k);
  411.                 if(c2 == c1){
  412.                     return true;
  413.                 }
  414.             }
  415.         }
  416.         return false;
  417.         }
  418.     public static boolean userContain(ArrayList<User> list,String name){
  419.         for (int l = 0; l < list.size(); l++) {
  420.             User user = new User();
  421.             user = list.get(l);
  422.             String rightName = user.getUserName();
  423.             if (rightName.equals(name)){
  424.                 return true;
  425.             }
  426.         }
  427.         return false;
  428.     }
  429.     private static boolean isRight(ArrayList<User> list,User user) {
  430.         String n = user.getUserName();
  431.         String p = user.getPassword();
  432.         for (int i = 0; i < list.size(); i++) {
  433.            User reuser = new User();
  434.            reuser = list.get(i);
  435.             if((reuser.getPassword().equals(p))&&(reuser.getUserName().equals(n))){
  436.                return true;
  437.            }
  438.         }
  439.         return false;
  440.     }
  441.     public static String getCode(){
  442.         ArrayList<Character> clist = new ArrayList<>();
  443.         for (int i = 0; i < 26; i++) {
  444.             clist.add((char)('a'+i)) ;
  445.             clist.add((char)('A'+i)) ;
  446.         }
  447.         StringBuilder sb = new StringBuilder();
  448.         Random random = new Random();
  449.         for (int l = 0; l < 4; l++) {
  450.             int index = random.nextInt(clist.size());
  451.             char c = clist.get(index);
  452.             sb.append(c);
  453.         }
  454.         int num = random.nextInt(10);
  455.         sb.append(num);
  456.         char[] chars = sb.toString().toCharArray();
  457.         for (int i = 0; i < chars.length; i++) {
  458.             int n = random.nextInt(chars.length);
  459.             char temp = chars[n];
  460.              chars[n] = chars[chars.length-1];
  461.              chars[chars.length-1] = temp;
  462.         }
  463.         return new String(chars);
  464.     }
  465.     public static void writeFile(ArrayList<?> list,String path) throws Exception{
  466.         int length = list.size();
  467.         ObjectOutputStream oos = null;
  468.         OutputStream ops = null;
  469.         for (int i = 0; i < length; i++) {
  470.             ops = new FileOutputStream(path,true);
  471.             oos = new ObjectOutputStream(ops);
  472.             oos.writeObject(list.get(i));
  473.             oos.writeObject('\n');
  474.         }
  475.         oos.flush();
  476.         oos.close();
  477.         ops.close();
  478.     }
  479.     public static void readFile(ArrayList<User> list1,ArrayList<Student> list2,String path,User user,Student student) throws Exception {
  480.         InputStream inputStream;
  481.         ObjectInputStream ois = null;

  482.         //String str;
  483.         try {
  484.             if(student == null) {
  485.                     inputStream = new FileInputStream(path);
  486.                    /* int len;
  487.                     byte[] b = new byte[1024];
  488.                     while((len = inputStream.read(b)) != -1){
  489.                          str = new String(b,0,len);
  490.                     }*/
  491.                     ois = new ObjectInputStream(inputStream);
  492.                     user = (User) ois.readObject();

  493.                 list1.add(user);

  494.             }
  495.         } catch (Exception e) {
  496.             e.printStackTrace();
  497.            /* System.out.println("没有任何数据,请先添加用户数据");
  498.             System.out.println("是否选择注册账户?(yes/no)");
  499.             String choice;
  500.             Scanner scanner = new Scanner(System.in);
  501.             choice = scanner.next();
  502.             if (choice.equalsIgnoreCase("yes")){
  503.                 register(Ulist);
  504.                 writeFile(Ulist,path1);
  505.             }else {
  506.                 System.out.println("默认退出");
  507.                 System.exit(0);
  508.             }*/
  509.         }
  510.         try {
  511.             if(user == null){
  512.                     inputStream = new FileInputStream(path);
  513.                     ois = new ObjectInputStream(inputStream);
  514.                     student= (Student) ois.readObject();
  515.                     list2.add(student);
  516.             }
  517.         } catch (Exception e) {
  518.             e.printStackTrace();
  519.            /* System.out.println("没有任何数据,请先添加学生数据");
  520.             System.out.println("是否选择添加学生信息?(yes/no)");
  521.             String choice;
  522.             Scanner scanner = new Scanner(System.in);
  523.             choice = scanner.next();
  524.             if (choice.equalsIgnoreCase("yes")){
  525.                 addStudent(Slist);
  526.                 writeFile(Ulist,path1);
  527.             }else {
  528.                 System.out.println("默认退出");
  529.                 System.exit(0);
  530.             }*/

  531.         }
  532.     }

  533. }

复制代码
  1. 用户类

  2. import java.io.Serializable;

  3. public class User implements Serializable {
  4.     public String userName;
  5.     private String password;
  6.     private String personId;
  7.     private String phoneNum;

  8.     public User() {
  9.     }

  10.     public User(String userName, String password, String personId, String phoneNum) {
  11.         this.userName = userName;
  12.         this.password = password;
  13.         this.personId = personId;
  14.         this.phoneNum = phoneNum;
  15.     }

  16.     public String getUserName() {
  17.         return userName;
  18.     }

  19.     public void setUserName(String userName) {
  20.         this.userName = userName;
  21.     }

  22.     public String getPassword() {
  23.         return password;
  24.     }

  25.     public void setPassword(String password) {
  26.         this.password = password;
  27.     }

  28.     public String getPersonId() {
  29.         return personId;
  30.     }

  31.     public void setPersonId(String personId) {
  32.         this.personId = personId;
  33.     }

  34.     public String getPhoneNum() {
  35.         return phoneNum;
  36.     }

  37.     public void setPhoneNum(String phoneNum) {
  38.         this.phoneNum = phoneNum;
  39.     }
  40. }
复制代码
  1. 学生类

  2. import java.io.Serializable;

  3. public class Student implements Serializable {
  4.       private  String name;
  5.       private  String id;
  6.       private  String address;
  7.       private  int age;

  8.     public Student(String name, String id, String address, int age) {
  9.         this.name = name;
  10.         this.id = id;
  11.         this.address = address;
  12.         this.age = age;
  13.     }

  14.     public Student() {
  15.     }

  16.     public String getName() {
  17.         return name;
  18.     }

  19.     public void setName(String name) {
  20.         this.name = name;
  21.     }

  22.     public String getId() {
  23.         return id;
  24.     }

  25.     public void setId(String id) {
  26.         this.id = id;
  27.     }

  28.     public String getAddress() {
  29.         return address;
  30.     }

  31.     public void setAddress(String address) {
  32.         this.address = address;
  33.     }

  34.     public int getAge() {
  35.         return age;
  36.     }

  37.     public void setAge(int age) {
  38.         this.age = age;
  39.     }
  40. }
复制代码

这代码文件路径我稍微改了一下
最佳答案
2022-9-26 15:16:37
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;

  5. public class Temp {
  6.     public static void main(String[] args) {
  7.         try {
  8.             BufferedReader bufferedReader = new BufferedReader(new FileReader("填写你的文本路径"));
  9.             while(bufferedReader.readLine() != null){
  10.                 System.out.println(bufferedReader.readLine());
  11.             }
  12.         } catch (FileNotFoundException e) {
  13.             e.printStackTrace();
  14.         } catch (IOException e) {
  15.             e.printStackTrace();
  16.         }
  17.     }
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-26 15:16:37 | 显示全部楼层    本楼为最佳答案   
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;

  5. public class Temp {
  6.     public static void main(String[] args) {
  7.         try {
  8.             BufferedReader bufferedReader = new BufferedReader(new FileReader("填写你的文本路径"));
  9.             while(bufferedReader.readLine() != null){
  10.                 System.out.println(bufferedReader.readLine());
  11.             }
  12.         } catch (FileNotFoundException e) {
  13.             e.printStackTrace();
  14.         } catch (IOException e) {
  15.             e.printStackTrace();
  16.         }
  17.     }
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-26 15:17:31 | 显示全部楼层
等一下不知道为毛自动翻译了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 03:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表