通用类
package shopmanager;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Linh
*/
public class Warehouse<T> {
private List<T> Product;
public void output() {
if (Product != null && !Product.isEmpty()) {
List<T> c;
for (int i = 0; i < Product.size(); i++) {
System.out.println(Product.get(i));
}
}
}
public void add(List<T> Product) {
this.Product = Product;
try {
save();
} catch (IOException ex) {
Logger.getLogger(Warehouse.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<T> getProduct() {
if (Product == null) {
Product = new ArrayList<T>();
}
return this.Product;
}
public void save() throws IOException{
FileOutputStream fs=new FileOutputStream("obj.dat");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(Product);
fs.close();
os.close();
}
}
如何将Generic类保存并加载到文件中
答案 0 :(得分:0)
序列化是将对象存储到持久存储区域(文件......等)的过程,通过使用反序列化,我们可以从文件中恢复对象
实现java.io.Serializable
请检查 http://www.tutorialspoint.com/java/java_serialization.htm