package 数仓4.demo; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class StudentAction { static Scanner sc=new Scanner(System.in); private static final String filepath = "D:\\astudent\\student.txt"; private File file; static ArrayList<Student> studentArrayList=new ArrayList<>(); static Student s1; ObjectOutputStream objectOutputStream=null; ObjectInputStream objectInputStream=null; public static void main(String[] args){ } public StudentAction(){ getStudents(); }//初始化 public void addstudent(){ System.out.println(studentArrayList.size()); Student stu =new Student(); System.out.print("id:"); stu.setId(sc.nextInt()); System.out.print("name:"); stu.setName(sc.next()); System.out.print("age:"); stu.setAge(sc.nextInt()); studentArrayList.add(stu); System.out.println("添加成功!"); }//增加学生 public void info1() { if (studentArrayList.size()==0) { addstudent(); return;} for (Student s:studentArrayList){ System.out.println(s); } System.out.println("遍历成功!"); }//遍历学生2 public void updata(Student stu){ for (Student s:studentArrayList){ if (s.getId()==stu.getId()){ s.setName(stu.getName()); s.setAge(stu.getAge()); } } System.out.println("修改成功!"); }//更新学生 public void remove(int id){ // getStudents(0); // File file =new File(filepath); // // for (Student s :studentArrayList){ // if (s.getId()==id){ // s1=s; // } // } // studentArrayList.remove(s1); // try { // objectOutputStream=new ObjectOutputStream(new FileOutputStream(file)); // } catch (IOException e) { // throw new RuntimeException(e); // } // try { // objectOutputStream.writeObject(studentArrayList); // } catch (IOException e) { // throw new RuntimeException(e); // } for (Student s : studentArrayList){ if (s.getId()==id){ s1=s; } } studentArrayList.remove(s1); System.out.println("删除成功!"); }//删除 public void getStudents() { file =new File(filepath); if (!file.exists()){ System.out.println(1); try { file.createNewFile(); } catch (IOException e) { throw new RuntimeException(e); } } if (file.length()==0){ System.out.println(2); try { objectOutputStream =new ObjectOutputStream(new FileOutputStream(filepath)); objectOutputStream.writeObject(new ArrayList<Student>()); } catch (IOException e) { throw new RuntimeException(e); } } try { System.out.println(3); objectInputStream =new ObjectInputStream(new FileInputStream(filepath)); studentArrayList= (ArrayList<Student>)objectInputStream.readObject(); System.out.println(7); } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); }finally { if (objectInputStream!=null){ try { objectInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }//初始化数组 public void setStudents(){ try { ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream(filepath)); objectOutputStream.writeObject(studentArrayList); } catch (IOException e) { throw new RuntimeException(e); } if (objectOutputStream!=null){ try { objectOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }//结束将集合输入文档 } // public List<Student> getAllStudents() throws IOException, ClassNotFoundException { // List<Student> students = new ArrayList<>(); // File file = new File(filepath); // if (!file.exists()) return students; // // try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { // while (true) { // try { // students.add((Student) ois.readObject()); // } catch (EOFException e) { // break; // 文件结束 // } // } // } // return students; // }
总结:
1.在Java中,当一个类(比如A类)调用另一个类(比如B类)的方法时,并不会自动执行B类的main
方法。main
方法是一个特殊的入口点,它仅在程序通过命令行或其他方式被明确指定为启动类时,由Java虚拟机(JVM)调用。
所以实现类似初始化功能时,最好将其放入无参构造方法中实现;
2.对于多IO操作可以将其封装为方法调用;
3.在Java中,增强for循环(也称为"for-each"循环)用于遍历数组或实现了Iterable
接口的集合(如List
、Set
等),但它在遍历过程中不直接提供对当前迭代元素的索引访问,也不允许你通过迭代变量来修改集合的大小(如添加或删除元素)。然而,关于能否在增强for循环中直接修改集合中对象的属性值,这是可以的,前提是这些对象是可变的,并且你没有试图通过迭代变量去添加或删除集合中的元素。
4.相同异常语句可以合并;
5。编程时要逻辑清晰,理清头绪再开始;