package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
public class ProcessFileObject {
protected String filename = "";
protected ProcessFileObject(String filename) {
this.filename = filename;
}
public ObjectOutputStream oos = null;
public ObjectInputStream ios = null;
public synchronized boolean insertAssignInfo(String assignid,
String assigncode, String assignname) {
ArrayList alist = new ArrayList();
Assign assign = new Assign();
assign.setAssigncode(assigncode);
assign.setAssignid(assignid);
assign.setAssignname(assignname);
/*try {
System.out.println(filename);
ios = new ObjectInputStream(new FileInputStream(new File(filename)));
alist = (ArrayList) ios.readObject();
ios.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (ClassNotFoundException cnfe) {
}*/
if (!alist.add(assign)) {
return false;
}
try {
oos = new ObjectOutputStream(new FileOutputStream(new File(filename)));
oos.writeObject(alist);
oos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return true;
}
public synchronized boolean existAssignInfo(String assignid,
String assigncode, String assignname) {
ArrayList alist = null;
Assign assign = null;
try {
ios = new ObjectInputStream(new FileInputStream(filename));
alist = (ArrayList) ios.readObject();
ios.close();
} catch (IOException ioe) {
} catch (ClassNotFoundException cnfe) {
}
Iterator iter = alist.iterator();
while (iter.hasNext()) {
assign = (Assign) iter.next();
if (assign.getAssigncode().equals(assigncode)
&& assign.getAssignid().equals(assignid)
&& assign.getAssignname().equals(assignname)) {
return true;
}
}
return false;
}
public synchronized boolean removebyAssigncode(String assigncode) {
ArrayList alist = null;
Assign assign = null;
try {
ios = new ObjectInputStream(new FileInputStream(filename));
alist = (ArrayList) ios.readObject();
ios.close();
} catch (IOException ioe) {
} catch (ClassNotFoundException cnfe) {
}
for (int i = 0, j = alist.size(); i < j; i++) {
assign = (Assign) alist.get(i);
if (assign.getAssigncode().equals(assigncode)) {
alist.remove(i);
}
}
try {
oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(alist);
oos.close();
} catch (IOException ioe) {
}
return true;
}
public synchronized boolean removeAll() {
ArrayList alist = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(alist);
oos.close();
} catch (IOException ioe) {
}
return true;
}
public class Assign implements Serializable {
public String assignid;
public String assigncode;
public String assignname;
public Assign() {
}
public String getAssigncode() {
return assigncode;
}
public void setAssigncode(String assigncode) {
this.assigncode = assigncode;
}
public String getAssignid() {
return assignid;
}
public void setAssignid(String assignid) {
this.assignid = assignid;
}
public String getAssignname() {
return assignname;
}
public void setAssignname(String assignname) {
this.assignname = assignname;
}
}
}