Entity
insertUserDao
@Override
public boolean insertUserDao(User user) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("TrainSystem\\User.txt",true));
String returnString=String.format("%s,%s,%s",user.getId(),user.getUserName(),user.getPassword());
bufferedWriter.write(returnString);
bufferedWriter.newLine();
bufferedWriter.close();
return true;
}catch (Exception e){
System.out.println("文件增加失败");
return false;
}
}
deleteUserDao
@Override
public boolean deleteUserDao(Integer id) {
try{
BufferedReader bufferedReader = new BufferedReader(new FileReader("TrainSystem\\User.txt"));
List<User> userList=new ArrayList<>();
String string;
while ((string=bufferedReader.readLine())!=null){
String[] dates=string.split(",");
User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2]);
userList.add(user);
}
for (int i = 0; i <=userList.size()-1 ; i++) {
User user=userList.get(i);
if (user.getId().equals(id)){
userList.remove(i);
}
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("TrainSystem\\User.txt"));
for (int i = 0; i <userList.size() ; i++) {
User user=userList.get(i);
String returnString=String.format("%s,%s,%s",user.getId(),user.getUserName(),user.getPassword());
bufferedWriter.write(returnString);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
return true;
}catch (Exception e){
System.out.println("数据删除失败");
return false;
}
}
updateUserDao
@Override
public boolean updateUserDao(User user, Integer id) {
try{
BufferedReader bufferedReader = new BufferedReader(new FileReader("TrainSystem\\User.txt"));
List<User> userList=new ArrayList<>();
String string;
while ((string=bufferedReader.readLine())!=null){
String[] dates=string.split(",");
User returnUser=new User(Integer.parseInt(dates[0]),dates[1],dates[2]);
System.out.println(returnUser);
userList.add(returnUser);
}
for (int i = 0; i <=userList.size()-1 ; i++) {
User returnUser=userList.get(i);
if (returnUser.getId().equals(id)){
userList.set(i,user);
}
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("TrainSystem\\User.txt"));
for (int i = 0; i <userList.size() ; i++) {
User returnUser=userList.get(i);
String returnString=String.format("%s,%s,%s",returnUser.getId(),returnUser.getUserName(),returnUser.getPassword());
bufferedWriter.write(returnString);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
return true;
}catch (Exception e){
System.out.println("数据更新失败");
return false;
}
}
selectUserDao
@Override
public List<User> selectUserDao() {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("TrainSystem\\User.txt"));
String string;
List<User> userList = new ArrayList<>();
while ((string = bufferedReader.readLine()) != null) {
String[] dates = string.split(",");
User user = new User(Integer.parseInt(dates[0]), dates[1], dates[2]);
userList.add(user);
}
bufferedReader.close();
return userList;
} catch (Exception e) {
System.out.println("数据查询失败");
return null;
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于