实现备份与还原,需用到的方法
来源:互联网 发布:人工智能编程 编辑:程序博客网 时间:2024/06/05 10:01
//写入文件
public void wirteFile(File f){
try {
bytes= null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(list);
oos.flush();
bytes = bos.toByteArray();
FileOutputStream outputStream=new FileOutputStream(f);
outputStream.write(bytes);
oos.close();
bos.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读文件
public void readFile(File f){
FileInputStream inputStream=new FileInputStream(f);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*5];
int length = -1;
while((length = inputStream.read(buffer)) != -1 ){
outStream.write(buffer, 0,length);
bytes=outStream.toByteArray();
}
outStream.close();
inputStream.close();
List newList=(List<Users>) toObject(bytes);
}
//bytes转换成对象
public Object toObject(byte[] bytes){
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
ObjectInputStream ois = new ObjectInputStream (bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
public void wirteFile(File f){
try {
bytes= null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(list);
oos.flush();
bytes = bos.toByteArray();
FileOutputStream outputStream=new FileOutputStream(f);
outputStream.write(bytes);
oos.close();
bos.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读文件
public void readFile(File f){
FileInputStream inputStream=new FileInputStream(f);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*5];
int length = -1;
while((length = inputStream.read(buffer)) != -1 ){
outStream.write(buffer, 0,length);
bytes=outStream.toByteArray();
}
outStream.close();
inputStream.close();
List newList=(List<Users>) toObject(bytes);
}
//bytes转换成对象
public Object toObject(byte[] bytes){
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
ObjectInputStream ois = new ObjectInputStream (bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
0 0
- 实现备份与还原,需用到的方法
- SQL2005还原时需用到的
- C#实现数据库的备份与还原
- C#实现数据库的备份与还原
- 数据库备份与还原的C#实现
- java实现Mysql的备份与还原
- sql2000备份的数据库还原到sql2005的方法
- 关于sqlserver的数据库备份与还原的方法
- 最简单的方法:mysql的备份与还原
- C#实现SQLSERVER2000数据库备份还原的两种方法
- 备份还原文件的方法
- mongodb备份还原的方法
- 用DOS命令备份与还原IP地址的方法
- 浅谈sqlserver2008备份与还原的方法一
- delphi中实现Sqlserver2000数据库的备份与还原!
- j2me实现手机通讯录的备份与还原
- Java实现SQLServer的数据库备份与还原
- Java实现Sqlserver及MySql的备份与还原
- OS 如何为UIView设置大小适宜的背景图片
- 顺时针打印矩阵
- POJ 3259 Wormholes
- redis info 参数说明
- 字符串表现形式区别
- 实现备份与还原,需用到的方法
- 刚刚结束的Java三次面试
- 获取时间
- AVL树
- 笔记32--目录values下xml文件:TypeArray、attrs、styles、colors、strings、dimens
- Struts2的工作原理
- 再论代码生成器
- mysql 表锁演示
- ZOJ 3498 Javabeans (二分)