用JAVA实现对txt文件文本增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA实现对txt文件文本增删改查相关的知识,希望对你有一定的参考价值。
package com.qiqiao.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class StudentManager { private static File DB = new File("students.db"); private static Set<Student> studentList = null; @SuppressWarnings("unchecked") private static void initStudentList() { if (DB.exists()) { FileInputStream fi = null; ObjectInputStream oi = null; try { fi = new FileInputStream(DB); oi = new ObjectInputStream(fi); Object obj = oi.readObject(); fi.close(); oi.close(); if (obj instanceof Set) studentList = (Set<Student>) obj; } catch (Exception e) { e.printStackTrace(); } } if (studentList == null) studentList = new HashSet<Student>(); } private static void save() { if (studentList == null) return; if (!DB.exists()) try { DB.createNewFile(); } catch (Exception e) { e.printStackTrace(); } FileOutputStream fo; try { fo = new FileOutputStream(DB); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(studentList); fo.close(); oo.close(); } catch (Exception e) { e.printStackTrace(); } } private static Student[] getStudents(int id, String name) { HashSet<Student> rlt = new HashSet<Student>(); if (id != 0) { for (Student s : studentList) { if (s.id == id) rlt.add(s); } } else if (name != null && !name.isEmpty()) { for (Student s : studentList) { if (s._name.equalsIgnoreCase(name)) rlt.add(s); } } return rlt.toArray(new Student[0]); } static { initStudentList(); } static void out(Object obj) { System.out.println(obj); } public static void menu() { out("Welcome to use Student Manager!\n1-search student;\t2-add student;\t3-list all students;\nothers-exit"); Scanner in = new Scanner(System.in); Student student = null; String input = in.nextLine(); if (input.equals("1")) { Student[] students = new Student[0]; out("please input name/id"); String ins = in.nextLine(); if (ins.matches("\\d+")) students = getStudents(Integer.parseInt(ins), null); else students = getStudents(0, ins); if (students.length != 0) { out(Arrays.toString(students).replaceAll("\\[|\\]|, ", "\n")); if (students.length == 1) student = students[0]; else { out("please choose a student by id:\n"); int i = in.nextInt(); for (Student s : students) { if (s.id == i) { student = s; break; } else { out("error"); menu(); } } } out("1-set score;\t2-delete score;\t3-delete student;\tother-exit"); int input1 = in.nextInt(); in.nextLine(); String course = null; float score = 0; switch (input1) { case 1: out("please input course"); course = in.nextLine(); out("please input score"); score = in.nextFloat(); in.nextLine(); student.setScore(course, score); break; case 2: out("please input course"); course = in.nextLine(); student.removeScore(course); break; case 3: student.removeFrom(studentList); break; } } else { out("NO current record!"); } } if (input.equals("2")) { out("please input id"); int id = in.nextInt(); in.nextLine(); out("please input name"); String name = in.nextLine(); out("please input class"); String cname = in.nextLine(); student = new Student(id, name, cname); if (studentList.add(student)) { out("create success!\n"); String input2 = "-"; while (!input2.isEmpty()) { out("set score? \n1-yes;\t2-no"); input2 = in.nextLine(); if (input2.equals("1")) { out("please input course"); String course = in.nextLine(); out("please input score"); float score = in.nextFloat(); student.setScore(course, score); in.nextLine(); } else break; } } else out("Error: id or student exists"); } if (input.equals("3")) { out(studentList.toString().replaceAll("\\[|\\]|, ", "\n")); } if (input.isEmpty()) return; save(); menu(); } public static void main(String[] args) { menu(); } } class Student implements Serializable { public String toString() { return "id: " + id + "\tclass: " + _class + "\tname: " + _name + courses.toString().replaceAll("\\{|\\}|, ", "\n").replaceAll( "=", ": "); } Student(int id, String _name, String _class) { this.id = id; this._class = _class; this._name = _name; } private static final long serialVersionUID = 1L; public int id = 0; public String _name = ""; public String _class = ""; HashMap<String, Float> courses = new HashMap<String, Float>(); void setScore(String course, float score) { courses.put(course, score); System.out.println("set success!\n\n" + this); } void removeScore(String course) { courses.remove(course); System.out.println("delete success!\n\n" + this); }; void removeFrom(Set<?> list) { list.remove(this); System.out.println("delete success!"); } public int hashCode() { return 31; } public boolean equals(Object obj) { Student s = null; if (obj instanceof Student) { System.out.println(1); s = (Student) obj; return s.id == id || (s._class.equals(_class) && s._name.equals(_name)); } return false; } } class DaJiangYou { DaJiangYou() { System.out.println("I am da jiang you"); } }
以上是关于用JAVA实现对txt文件文本增删改查的主要内容,如果未能解决你的问题,请参考以下文章