2019年3月8日,序列化
java源文件分享地址:
链接:https://pan.baidu.com/s/10susCDfNmjYsd3pTlzbrcg
提取码:7y20
复制这段内容后打开百度网盘手机App,操作更方便哦
作业一:
编写代码为:
package com.zuoye; import java.io.*; public class PetTest { public static void main(String[] args) { FileInputStream fis = null; InputStreamReader is = null; BufferedReader br = null; Writer fw= null; BufferedWriter bw = null; try { //读取pet.template文件中的信息 fis = new FileInputStream("g:\\新建文件夹\\pet.template"); is = new InputStreamReader(fis,"GBK"); br = new BufferedReader(is); String line = ""; StringBuffer sbf = new StringBuffer(); while ((line = br.readLine())!=null){ sbf.append(line); } System.out.println("替换前:"+sbf.toString()); //打印读取的信息 //替换名词 String result = sbf.toString().replace("{name}","旺财").replace("{type}","拉布拉多犬").replace("{master}","张三"); System.out.println("替换后:"+result); //将读取的宠物信息写入新的pet.txt文件中 fw = new FileWriter("g:\\新建文件夹\\pet.txt"); bw = new BufferedWriter(fw); bw.write(result); System.out.println("写入新文件成功!"); bw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { bw.close(); fw.close(); br.close(); is.close(); fis.close(); // re.close(); } catch (IOException e) { e.printStackTrace(); } } } }
输出结果为:
替换前:您好!我的名字是{name},我是一只{type}。我的主人是{master}
替换后:您好!我的名字是旺财,我是一只拉布拉多犬。我的主人是张三
写入新文件成功!
作业二:
用序列化和反序列化 写一个可以多次注册,并且可以用注册过的所有用户登录的功能,要求每次注册一个用户,当再次注册用户时,不能把之前注册过的用户覆盖掉
编写代码为:
1、用户类:
package com.zuoye.zuoye; import java.io.Serializable; public class User implements Serializable { private int id; private String name; private String password; private String phone; public User() { } public User(int id, String name, String password, String phone) { this.id = id; this.name = name; this.password = password; this.phone = phone; } 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
2、注册和登录方法类
package com.zuoye.zuoye; import com.sun.corba.se.impl.orbutil.ObjectWriter; import java.io.*; import java.util.*; public class Register { List<User> list = new ArrayList<User>(); Scanner input = new Scanner(System.in); public void zhuCe() { System.out.print("请输入用户名:"); String name = input.next(); System.out.print("请输入密码:"); String password = input.next(); System.out.print("请输入您的手机号:"); String phone = input.next(); int id = 0; Boolean flag = false; do{ Random random = new Random(); id = random.nextInt(10000); if(id>999){ for(int i = 0 ;i<list.size();i++){ if(id==list.get(i).getId()){ flag = true; break; } } break; }else { flag =true; } }while (flag); User user = new User(id,name,password,phone); list.add(user); //新建文件,用于保存注册信息 // String fileName = Integer.toString(user.getId()); // File file = new File("g:\\test",fileName+".txt"); // // try { // file.createNewFile(); // } catch (IOException e) { // e.printStackTrace(); // } FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream("g:\\test\\user.txt"); oos = new ObjectOutputStream(fos); oos.writeObject(list); System.out.println("注册成功!您的会员号为:"+user.getId()); oos.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { oos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } public void dengRu(){ int id = 0; do{ System.out.print("请输入注册ID号:"); Scanner input1 = new Scanner(System.in); try { id = input1.nextInt(); break; }catch (Exception e){ System.out.println("输入有误!"); } }while (true); System.out.println("请输入登录密码:"); String password = input.next(); FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream("G:\\test\\user.txt"); ois = new ObjectInputStream(fis); ArrayList<User> list1 = (ArrayList)ois.readObject(); Boolean login = false; for(int i = 0; i<list1.size();i++){ User user1 = list1.get(i); if(id==user1.getId()&&password.equals(user1.getPassword())){ login = true; break; } } System.out.println(login?"登入成功!":"登入失败!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { try { ois.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3、测试类:
package com.zuoye.zuoye; import java.util.Scanner; public class RegisterTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); Register register = new Register(); do{ System.out.println("请选择功能:1、登录 2、注册"); String choice = input.next(); switch (choice){ case "1": register.dengRu(); break; case "2": register.zhuCe(); break; default: System.out.println("输入有误!"); } }while (true); } }
输出结果为:
"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" "-javaagent:G:\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=51904:G:\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_191\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\rt.jar;G:\IO流\out\production\IO流" com.zuoye.zuoye.RegisterTest 请选择功能:1、登录 2、注册 1 请输入注册ID号:6014 请输入登录密码: 888888 登入成功! 请选择功能:1、登录 2、注册 2 请输入用户名:admin 请输入密码:666666 请输入您的手机号:18888888888 注册成功!您的会员号为:4688 请选择功能:1、登录 2、注册 2 请输入用户名:user 请输入密码:12345678 请输入您的手机号:18888888888 注册成功!您的会员号为:4783 请选择功能:1、登录 2、注册 1 请输入注册ID号:4783 请输入登录密码: 12345678 登入成功! 请选择功能:1、登录 2、注册