技术需求:
1、将数据库一条记录数据转成一个物理文件,n条记录可以生成n个文件。
2、要求物理文件不能存有明码,也就是说用记事本之类的程序打开是乱码(不希望用加密技术,能存二进制格式吗?)
3、需将物理文件里的数据映射成一个实体类,例如读取该文件并生成一个Product的实体对象。数据表:table_productid name category price barcode producer description
------------------------------------------------------------------------------------------------
1 美的空调 2 5800.00 xxxxxxxxx-x 美的电器公司 该产品的描述(字数在500字以内)
2 Lenovo-460I笔记本 3 4200.00 xxxxxxxxx-x 联想科技公司 ......
3 2009款2.5V丰田皇冠 1 326800.00 xxxxxxxxx-x 一汽丰田公司 .......实体类:Product.javapublic class Product{ private Integer id;
private String name;
private Category category;
private BigDecimal price;
private String barcode;
private String producter;
private String description; setter && getter......
}
1、将数据库一条记录数据转成一个物理文件,n条记录可以生成n个文件。
2、要求物理文件不能存有明码,也就是说用记事本之类的程序打开是乱码(不希望用加密技术,能存二进制格式吗?)
3、需将物理文件里的数据映射成一个实体类,例如读取该文件并生成一个Product的实体对象。数据表:table_productid name category price barcode producer description
------------------------------------------------------------------------------------------------
1 美的空调 2 5800.00 xxxxxxxxx-x 美的电器公司 该产品的描述(字数在500字以内)
2 Lenovo-460I笔记本 3 4200.00 xxxxxxxxx-x 联想科技公司 ......
3 2009款2.5V丰田皇冠 1 326800.00 xxxxxxxxx-x 一汽丰田公司 .......实体类:Product.javapublic class Product{ private Integer id;
private String name;
private Category category;
private BigDecimal price;
private String barcode;
private String producter;
private String description; setter && getter......
}
保存到文件时,直接将Product的实例 用ObjectOutputStream 将对象实例序列化输出到文件
看6楼给的
:id.xml在xml中保存个列的节点。
字段内容使用Base64Convert 加密:
public class Base64Convert { BASE64Decoder decoder = new BASE64Decoder(); public String ioToBase64(String strPath) throws IOException {
String fileName = strPath; // 源文件
String strBase64 = null;
try {
InputStream in = new FileInputStream(fileName);
// in.available()返回文件的字节长度
byte[] bytes = new byte[in.available()];
// 将文件中的内容读入到数组中
in.read(bytes);
strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
in.close();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("str:"+strBase64);
return strBase64;
} public void base64ToIo(String strBase64,String strPath) throws IOException {
String string = strBase64;
String fileName = strPath; // 生成的新文件
try {
// 解码,然后将字节转换为文件
byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[1024];
FileOutputStream out = new FileOutputStream(fileName);
int bytesum = 0;
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
out.write(buffer, 0, byteread); // 文件写操作
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
一个从文件实例化对象的关键类
/**
* Package : com.parse.lei
* File : GeneratorObject.java
* Company : ASPire Technologies, Inc.
* Tem : MQMS Develop Team
* Version : v1.0
* Description : <Detail Description for package>
*
* Copyright (c) 2003-2006 ASPire Technologies, Inc.
* 6/F,IER BUILDING, SOUTH AREA,SHENZHEN HI-TECH INDUSTRIAL PARK Mail Box:11# 12#.
* All rights reserved.
* The contents of this file are confidential and proprietary to ASP.
* Copying is explicitly prohibited without the express permission of ASP.
*
* Create Date : Nov 9, 2009
* Create By : x_taihaiping
* History :$
*/
package cn.util;import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;import net.sf.cglib.beans.BulkBean;import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;/**
* 生成对象 File Name : GeneratorObject.java
*
* @Description : <Detail Description for Class >
* @author x_taihaiping
*/
public class GeneratorObject {
private BulkBean bulkBean;
private static Logger logger = Logger.getLogger(GeneratorObject.class); /**
* 初始化
* Description : <Detail Description for method>
*
* @param target
*
*/
private void init(Class<?> target) {
Field[] fields = target.getDeclaredFields();
int fieldCount = fields.length;
String[] getters = new String[fieldCount];
String[] setters = new String[fieldCount];
Class<?> types[] = new Class[fieldCount];
try {
for (int i = 0; i < fieldCount; i++) {
Field tem = fields[i];
String ziduan = tem.getName();
getters[i] = "get" + ParseUtil.upperCaseFirstChar(ziduan);
setters[i] = "set" + ParseUtil.upperCaseFirstChar(ziduan);
types[i] = tem.getType();
}
} catch (Exception e) {
logger.error("类没有找到.." + e.getMessage());
}
this.bulkBean = BulkBean.create(target, getters, setters, types);
} /**
* 创建对象 Description : <Detail Description for method>
*
* @param target
* @param fileMeta
* @param values
* @return
*
*/
private Object createObject(Class<?> target, Object[] values) {
Object targetObject = null;
try {
targetObject = target.newInstance();
} catch (Exception e) {
logger.error("创建对象出错,目标类-->" + target.getName(), e);
}
// Class[] types = bulkBean.getPropertyTypes();
// Object[] valu = new Object[types.length];
// for(int i=0;i<types.length;i++)
// {
// valu[i] = values[i];
// System.out.println(types[i].getName());
// if(types[i].getName().equals("int"))
// {
// valu[i] = Integer.valueOf(values[i].toString()).intValue();
// }
// }
bulkBean.setPropertyValues(targetObject, values);
return targetObject;
} /**
* 实例化对象
* Description : <Detail Description for method>
*
* @param target
* @param dataFile
* @return
*
*/
@SuppressWarnings( { "unchecked", "static-access" })
public List createObjects(Class<?> target, String path) {
// 初始化BulkBean
init(target);
List result = new ArrayList();
LineIterator iterator = null;
File dataFile = new File(path);
try {
iterator = FileUtils.lineIterator(dataFile);
while (iterator.hasNext()) {
String line = iterator.nextLine();
// 过滤掉数据文件中的空行
if (StringUtils.isBlank(line)) {
continue;
}
Object[] values = this.parseLine(line);
Object object = createObject(target, values);
result.add(object);
}
} catch (IOException e) {
logger.error("读取文件名出错!-->" + dataFile.getName(), e);
} finally {
LineIterator.closeQuietly(iterator);
}
return result;
} /**
* 文件中单行解析
*
* @param line
* @param fileMeta
* @return
*/
public static Object[] parseLine(String line) {
Object[] obj = null;
obj = new DefaultLineSpliter().split(line);
return obj;
} @SuppressWarnings("unchecked")
public static void main(String[] arg) {
String path = "D:/stu.txt";
List ret = new GeneratorObject().createObjects(Poeple.class, path);
System.out.print(((Poeple) ret.get(1)).getSex());
}
}