public class Model { private File file; private ArrayList<Point> points; private ArrayList<ModelListener> listeners; public Model() { file = new File("points.txt"); points = new ArrayList<Point>(); try { FileInputStream fi = new FileInputStream(file); ObjectInputStream in = new ObjectInputStream(fi); for(Point p:points) { p = (Point) in.readObject(); } in.close(); fi.close(); }catch (IOException | ClassNotFoundException i) { System.out.println(i.getMessage()); } listeners = new ArrayList<ModelListener>(); } public void savaData() { try { FileOutputStream fo = new FileOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(fo); for(Point p:points) { out.write(p.x); out.write(p.y); out.close(); fo.close(); } }catch (IOException i) { System.out.println(i.getMessage()); } }
时间: 2023-12-06 20:42:16 浏览: 55
这是一个简单的 Model 类,用于读取和保存点的坐标数据到文件。在构造函数中,它会从文件中读取 Point 对象并添加到 points 列表中。在 saveData() 方法中,它会把 points 列表中的点数据写入到文件中。
需要注意的是,在读取文件时,应该先创建一个空的 Point 对象,然后使用 in.readObject() 方法读取 Point 对象,并将其添加到 points 列表中。在保存文件时,应该使用 ObjectOutputStream 对象进行写操作,并使用 out.writeObject(p) 方法将 Point 对象写入文件中。
此外,该类还使用了观察者模式,即 listeners 列表用于保存所有注册的 ModelListener 对象,以便在数据发生变化时通知它们。
相关问题
public class Model { private File file; private ArrayList<Point> points; private ArrayList<ModelListener> listeners; public Model() { file = new File("points.txt"); points = new ArrayList<Point>(); try { FileInputStream fi = new FileInputStream(file); ObjectInputStream in = new ObjectInputStream(fi); for(Point p:points) { p = (Point) in.readObject(); } in.close(); fi.close(); }catch (IOException | ClassNotFoundException i) { System.out.println(i.getMessage()); } listeners = new ArrayList<ModelListener>(); } public void savaData() { try { FileOutputStream fo = new FileOutputStream(file); ObjectOutputStream out = new ObjectOutputStream(fo); for(Point p:points) { out.writeObject(p); out.close(); fo.close(); } }catch (IOException i) { System.out.println(i.getMessage()); } }
这是一个Java类,名为Model,包含了一些成员变量和方法。它的作用是用来读取和保存文件中的点对象。
其中,file是一个File类型的变量,代表了要读取和保存的文件;points是一个ArrayList<Point>类型的变量,用来存储从文件中读取到的点对象;listeners是一个ArrayList<ModelListener>类型的变量,用来存储该类的监听器。
在构造函数中,Model会先通过file对象创建一个FileInputStream,再通过ObjectInputStream将文件中的点对象读取到points中。
在savaData方法中,Model会先通过file对象创建一个FileOutputStream,再通过ObjectOutputStream将points中的点对象写入到文件中。
需要注意的是,在读取文件和保存文件的时候,需要使用try-catch语句来处理IOException和ClassNotFoundException异常。同时,在写入文件时,需要对每个点对象都调用out.writeObject(p)方法,并在方法结束后关闭流。
根据以下要求:Instead of using a binary file to save the arraylist of points, change the savaData method and the constructor of the Model class to use a database to write / read the coordinates of all the points. Use XAMPP and phpMyAdmin to create a database called "java" with a table called "points" that has two integer columns x and y (in addition to the ID primary key). Hint: make sure you delete all the old point coordinates from the database before inserting new ones. Hint: use phpMyAdmin to check what is stored in the database。修改下述代码:public class Model implements Serializable { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; private static final String FILE_NAME = "points.bin"; public Model() { points = new ArrayList<Point>(); listeners = new ArrayList<ModelListener>(); // Read points from file if it exists File file = new File(FILE_NAME); if (file.exists()) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); points = (ArrayList<Point>) in.readObject(); in.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { points.add(p); notifyListeners(); // points changed so notify the listeners. saveData(); // save point to file } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. saveData(); // save empty list to file } public void deleteLastPoint() { if (points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. saveData(); // save updated list to file } } private void notifyListeners() { for (ModelListener l : listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public void saveData() { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME)); out.writeObject(points); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
修改后的代码如下所示,主要是将数据存储方式从二进制文件改为了使用数据库。具体实现是创建了一个名为"java"的数据库,并在其中创建了一个名为"points"的表,该表有三个列,分别是ID、x和y,其中ID是主键。在saveData()方法中,使用JDBC连接数据库,并将所有点的坐标存储到"points"表中。在Model类的构造函数中,读取数据库中保存的所有点的坐标,并将其存储在points列表中。
```
import java.sql.*;
import java.util.ArrayList;
public class Model implements Serializable {
private ArrayList<Point> points;
private ArrayList<ModelListener> listeners;
private static final String DB_URL = "jdbc:mysql://localhost:3306/java";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
// Read points from database
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM points");
while (rs.next()) {
int x = rs.getInt("x");
int y = rs.getInt("y");
points.add(new Point(x, y));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addListener(ModelListener l) {
listeners.add(l);
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
points.add(p);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save point to database
}
public void clearAllPoints() {
points.clear();
notifyListeners(); // points changed so notify the listeners.
saveData(); // save empty list to database
}
public void deleteLastPoint() {
if (points.size() > 0) {
points.remove(points.size() - 1);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save updated list to database
}
}
private void notifyListeners() {
for (ModelListener l : listeners) {
l.update(); // Tell the listener that something changed.
}
}
public int numberOfPoints() {
return points.size();
}
public void saveData() {
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM points");
for (Point p : points) {
stmt.executeUpdate("INSERT INTO points (x, y) VALUES (" + p.x + ", " + p.y + ")");
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
阅读全文