对保存在txt文件中的一个Object类对象的增删改方法实现的总结
1. 假设
这个Object具有以下属性(可能还有其它属性,这里举例便于理解就不写多了):
id,name,price;
如果如果这个对象是图书,那么对图书的增删改应该是图书管理员的操作
,所以,可以将增删改定义成为一个接口,对应的方法就应该是:
interface IMgr{
Object getObjectList();
Object get(int id);
boolean addObj(Object o);
boolean delObj(int id);
boolean updateObj(Object o);
}
2. 接口IMgr的实现类
class IMgr_impl implements IMgr{
public Object getObjectList(){}
public Object get(int id){}
public boolean addObj(Object o){}
public boolean delObj(int id){}
public boolean updateObj(Object o){}
}
3. getObjectList()方法的实现:
@Override
public List<Object> getObjectList() {
List<Object> objectList = new ArrayList<>();
BufferedReader br = null;
try {
br = new BufferedReader(
new FileReader(MyUtil.getPath())
);
String str = null;
while ((str = br.readLine()) != null) {
String fileDataList[] = str.split("\\s+");
int id = Integer.parseInt(fileDataList[0]);
String name = fileDataList[1];
String price = fileDataList[2];
Object tempObj = new(id,name, price);
objectList .add(tempObj );
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return objectList ;
}
4. get(int id)方法的实现:
@Override
public Object get(int id) {
Object o = null;
BufferedReader br = null;
try {
br = new BufferedReader(
new FileReader(MyUtil.getPath())
);
String str = null;
while ((str = br.readLine()) != null) {
String fileDataList[] = str.split("\\s+");
int tempId = Integer.parseInt(fileDataList[0]);
if (i == tempId) {
o = new Object();
o.setId(tempId);
o.setName(fileDataList[1]);
o.setPrice(fileDataList[2]);
}
return o;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
5. addObj(Object o)方法的实现:
@Override
public boolean addObj(Object o) {
List<Object> objectList = getObjectList();
if (o != null && o.getId() > 0) {
objectList.add(o);
PrintWriter out= null;
try {
out= new PrintWriter(MyUtil.getPath());
for (Object obj : objectList) {
String str = obj.getId() + " " +
obj.getName() + " " +
obj.gethPrice();
out.println(str);
out.flush();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
return false;
}
6. delObj(int id)方法的实现:
@Override
public boolean delHouse(int id) {
List<Object> objectList = getObjectList();
boolean isExisted = false;
for (int i = 0; i < objectList.size(); i++) {
Object o = objectList.get(i);
if (id == o.getId()) {
isFind = true;
houseList.remove(o);
break;
}
}
if (isExisted) {
PrintWriter out = null;
try {
out = new PrintWriter(MyUtil.getPath());
for (Object obj: objectList) {
String str = obj.getId() + " " +
obj.getName() + " " +
obj.getPrice() ;
out .println(str);
out.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out .close();
}
}
return true;
}
return false;
}
7. updateObj(Object o)方法的实现:
@Override
public boolean updateObj(Object o) {
List<Object> objectList = getObjectList();
int index = -1;
boolean isExisted = false;
for (int i = 0; i < objectList.size(); i++) {
if (o.getId() == objectList.get(i).getId()) {
isExisted = true;
index = i;
break;
}
}
if (isExisted) {
objectList.get(index).setId(o.getId());
objectList.get(index).setName(o.getName());
objectList.get(index).setPrice(o.getPrice());
PrintWriter out = null;
try {
out = new PrintWriter(MyUtil.getPath());
for (Object obj : objectList) {
String str = obj.getId() + " " +
obj.getName() + " " +
obj.getPrice();
out.println(str);
out.flush();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
out .close();
}
}
}
return false;
}
8. 关于MyUtil.getPath()方法的实现:
因为所有给增删改操作都涉及到文件的读取,很多地方都需要获取文件路径,所以可以将这个方法写在MyUtil这个工具类当中,需要用的时候,直接调用方法就好了
1. 这个类需要用到的java包:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
2. 文件夹:
在项目文件下新建文件夹res,将Object.txt放在里面
然后需要在res文件夹里面创建一个path.properties

在path.properties里面写:filepath=res/object.txt
3. 实现:
public class MyUtil {
private static final String PATH = "res/path.properties";
private static final String FILE_PATH_KEY = "filepath";
public static String getPath() {
Properties pps = new Properties();
InputStream is = null;
Object o = null;
try {
is = new FileInputStream(PATH);
pps.load(is);
o = pps.getProperty(FILE_PATH_KEY);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return o.toString();
}
}