1、对文件pet.template中的肉容进行替换,再输出到文件中
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();
}
}
}
}
替换前文本内空:
控制台输出:
替换后:
2、 用序列化和反序列化 写一个可以多次注册,并且可以用注册过的所有用户登录的功能
要求每次注册一个用户,当再次注册用户时,不能把之前注册过的用户覆盖掉
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 + "]";
}
}
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("登录失败!");
}
}
}
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();
}
}
}
}
用户几号信息记录文本:
结果示例:当前无帐号信息无法登录
注册帐号:
登录测试:
可以看到当前文本中有信息:
两个注册的帐号都可以登录
未注册的帐号无法登录