-
package
import
import
tFoundException;
import
tputStream;
import
ption;
import
Stream;
import
nt;
import
t;
import
import
putter;
public
class
FileDemo {
/**
*
@param
args
*/
public
static
void
main(String[] args) {
//
TODO
Auto-generated method
stub
String filename=
;
File
file=
new
File(filename);
/**
*
判断文件是否存在
.
如果文件不存在则生成一个文件
*
则生成一个文件;反之则先删除原有文件,生成一个新的文件
*/
if
(()){
try
{
();
}
catch
(Exception e) {
//
TODO
Auto-generated catch
block
tackTrace();
}
}
try
{
NewFile();
}
catch
(IOException e) {
//
TODO
Auto-
generated catch block
tackTrace();
}
//
创建根节点
Element root=
new
Element(
);
//
根节点添加到文档中
Document
document=
new
Document(root);
//
添加工单基本信息
//
创建工单基本信息的节点
Element
baseinfo=
new
Element(
);
//
创建工单编号节点
Element
formOid=
new
Element(
);
//
添加工单编号节点内容
t(<
/p>
工单编号
);
//<
/p>
将工单编号节点添加到基本信息节点下
tent(formOid);
//
创建任务名称节点
Element
taskName=
new
Element(
);
//
添加任务名称内容
t(
任务名称
);
//
将任务名称添加到基本信息中
tent(taskName);
//
创建工单类型
Element
leafletType=
new
Element(
);
t(
工单类型
);
tent(leafletType);
//
涉及网元
Element
metadataName=
new
Element(
);
t(
涉及网元
);
tent(metadataName);
//
申请人
Element
applicant=
new
Element(
);
t(
申请人
);
tent(applicant);
//
申请时间
Element
applyTime=
new
Element(
);
t(
申请时间
);
tent(applyTime);
//
问题描述
Element
problemDesc=
new
Element(
);
t(
申请时间
);
tent(problemDesc);
//
附件
Element
accessory=
new
Element(
);
t(
附件
);
tent(accessory);
//
调整原因
Element
AdjustReason=
new
Element(
);
//
创建调整原因的根节点
Element items=
new
Element(
);
//
创建一个原因信息
Element item=
new
Element(
);
//
小区名
Element
villagename=
new
Element(
);
t(
小区名
);
tent(villagename);
//LAC
Element
lac=
new
Element(
);
t(
);
tent(lac);
//CI
Element ci=
new
Element(
);
t(
);
tent(ci);
//bsc
Element bsc=
new
Element(
);
t(
);
tent(bsc);
}
}
//msc
Element
msc=
new
Element(
);
t(
);
tent(msc);
//
行政区域
Element
canton=
new
Element(
);
t(
);
tent(canton);
//
设备类型
Element
devicetype=
new
Element(
);
t(
设备类型
);
tent(devicetype);
//
问题时间
Element
problemtime=
new
Element(
);
t(
问题时间
);
tent(problemtime);
//
指标或参数名
Element
parametername=
new
Element(
);
t(
指标或参数名
);
tent(parametername);
//
指标或参数值
Element
parametervalues=
new
Element(
);
t(
指标或参数值
);
tent(parametervalues);
tent(item);
tent(items);
tent(AdjustReason);
//
将工单编号节点添加到根节点下
tent(baseinfo);
//
设置
xml
文件格式
---
选用这用格式可以是生成的
xml
文件自动换行
同时缩进
Format format=Format.
p>
getPrettyFormat
();
XMLOutputter
xmlout=
new
XMLOutputter(format);
OutputStream out;
try
{
out =
new
FileOutputStream(file,
true
);
//
将内容写到文件中
(document, out);
}
catch
(Exception e) {
//
TODO
Auto-generated catch
block
tackTrace();
}
推荐群组
: JSF
更多相关推荐
最近的项目中用到了<
/p>
XML
,所以就写了个对象转换为
XML
文件的通用类。
Java
代码
package
import
import tionTargetException;
import
import
DateFormat;
import p;
import
/**
*
* Description:
反射机制工具类,根据传入的
Obj
,
*
为生成
xml
文件提取所有属性和属性对应的值
*
*
*
* @author Administrator
* @since
2008-12-1
* @version
*/
public class ReflectionUtil {
//
所有的系统类型
private static
String[] allSysDefinedType = new String[] {
private static
String dateFormatString =
//<
/p>
使用不可以是属性名前缀的
*
作为标识是
否是用户自定义的类型
private static String
userDefiendTypePrefix =
/**
*
根据传入的对象将对象的属性和该属性对应的值放入
map
* @param
obj
*
待映射属性和属性值的对象
* @param
unReflectProp
*
不需要映射属性和属性值的所有属性;
*
eg. CbsTargetTime
对象中有
DATE_FORMAT
不需要映射则在该参数中添
加
*
TE_FORMAT
*
* @return Map
*
key = property name value
= property value;
*
(
p>
如果属性为一个用户自定义类型的对象那么
*
key = '*' + 'user defined
type';
*
value = map;map
中
key
是属性,
value
是值,依次循环)
*/
public Map
String[] unReflectProp) {
Map
Map
(userDefiendTypePrefix +
ss().getSimpleName(),
dataMap); //
使用不可以是属性名前缀的
*
作为标识是否是用户自定义的类型
Pro(obj,
unReflectProp, dataMap, ss().getName());
return rootMap;
}
/**
*
得到传入对象的每个属性域,
*
根据每个属性域得到属性域对应的值
*
* @param obj
*
待映射得到属性和值的对象
*
* @param
unReflectProp
*
不需要映射属性和属性值的所有属性;
*
* @param
className
*
对象的类型名,如果传入对象为空时,用其新建一个对象
* @param
dataMap
*
p>
key=
属性,
value=
属性的值
*/
@SuppressWarnings(
private void
getObjPro(Object obj, String[] unReflectProp, Map
dataMap,
String className) {
try {
Class clas;
if (obj == null) {
clas =
e(className);
obj
= tance();
}
else {
clas = e(ss().getName());
}
//<
/p>
得到
obj
类的所有属性
Field[] fileds = laredFields();
for (Field
field : fileds) {
String fieldName = e();
String fieldType =
e().getName();
//<
/p>
如果该属性是不需要映射的属性则跳出循环
if
(!reflectPropOrNot(ss().getSimpleName() +
+ fieldName,
unReflectProp)) {
continue;
}
//<
/p>
属性名的第一个字母大写,与
get
或者
is
组成方法名
String
firstCharUpper = ing(0, 1).toUpperCase()
+ ing(1, ());
Method method;
//
如果
是
boolean
型则方法名为
is*
;反之为
get*
if
(isBooleanType(fieldType)) {
method = hod(
} else {
method = hod(
}
if
(isSysDefinedType(fieldType)) {
//
如果是系统类型则添加进入
map
String
formatDateStr = isTimeType(fieldType, method,
obj);
if
(formatDateStr != null) {
(fieldName, formatDateStr);
continue;
}
(fieldName,
(obj, null) == null ?
.invoke(obj, null));
} else {
//
如果不是系统类型对象则
key
= * +
属性名
;
value =
map
;循环迭代
Map dateMap2 = new
HashMap();
Pro((obj, null), unReflectProp,
dateMap2,
fieldType);
(userDefiendTypePrefix
+ e().getSimpleName(),
dateMap2);
}
}
}
catch (Exception e) {
// TODO Auto-generated catch block
n(
映射
属性和属性所对应值出现异常!
!
tackTrace();
}
}
/**
*
验证属性是否是要通过反射机制得到名称和值
* @param
fieldName
*
待验证的属性
* @return
*
true
要利用反射机制的到属性名和值
*
false
不需要利用反射机制的到属性名和值
*/
private boolean reflectPropOrNot(String
fieldName, String[] unReflectProp) {
if (unReflectProp == null)
{
return true;
}
for (String propName :
unReflectProp) {
if ((fieldName)) {
return false;
}
}
return true;
}
/**
*
验证属性的类型是否是系统定义的类型
* @param
fieldType
*
待验证类型的名称
* @return
*
true
是系统定义的对象
*
false
用户定义的对象
*/
private boolean isSysDefinedType(String
fieldType) {
for
(String type : allSysDefinedType) {
if ((type)) {
return true;
}
}
return false;
}
/**
*
如果该属性是日期属性则根据要
输出的类型返回字符串,反之返回
null
* @param fileType
* @param method
* @param obj
* @return
* @throws
IllegalAccessException
* @throws InvocationTargetException
*/
private String
isTimeType(String fileType, Method method, Object
obj)
throws IllegalAccessException,
InvocationTargetException {
if ((
|| (
if ((obj, null) == null) {
return null;
}
SimpleDateFormat sdf = new
SimpleDateFormat(dateFormatString);
return ((obj, null));
} else {
return null;
}
}
public boolean isBooleanType(String
fieldType) {
if
((
|| (
-
-
-
-
-
-
-
-
-
上一篇:部编版初中语文全册字词
下一篇:中考优秀作文30篇范例