您的位置:首页 > 编程语言 > Java开发

Java生成XML文件-封装类

2012-09-29 11:00 453 查看
import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import com.gzdec.common.config.AppConfig;

import com.gzdec.common.web.exception.DefaultException;

public class WriteXML {

static File cgiPath = null;

static String root = null;

public void writeXML(String path,String fileName,String xml) throws IOException, DefaultException{

/*创建目录*/

File filePath = createDir(root,path.trim());

/*创建文件*/

File file = getFile(filePath, fileName.trim() + ".xml");

/*写文件*/

writeFile(file,xml);

}

static {

try {

root = AppConfig.getProperty("rootPath") + "/lcms/statistic/indexjs";

cgiPath = createDir(AppConfig.getProperty("rootPath"),AppConfig.getProperty("cgiFilePath"));

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 创建目录,目录必须以"/"间隔

* @param rootPath

* @param path

* @throws IOException

* @throws DefaultException

*/

public static File createDir(String rootPath,String path) throws IOException, DefaultException{

File file = new File(rootPath);

if(!file.exists()) {

throw new DefaultException("根目录不存在");

}

String dirs[] = path.split("/");

for(int i=0;i<dirs.length;i++){

if(dirs[i]!=null && !"".equals(dirs[i].trim())){

file = getDir(file,dirs[i]);

}

}

return file;

}

/**

* 创建目录

* @param parentPath

* @param dirName

* @return

* @throws IOException

*/

public static File getDir(File parentPath, String dirName)

throws IOException {

File dir = new File(parentPath, dirName);

if (!dir.exists()) {

dir.mkdir();

}

return dir;

}

/**

* 创建文件

* @param dirPath

* @param fileName

* @return

* @throws IOException

*/

public static File getFile(File dirPath, String fileName)

throws IOException {

File file = new File(dirPath, fileName);

if (!file.exists()) {

file.createNewFile();

}

return file;

}

/**

* 写文件

* @param file

* @param html

* @throws IOException

* @throws DefaultException

*/

public static void writeFile(File file , String xml) throws IOException, DefaultException{

if(xml!=null && !"".equals(xml.trim())){

/* 参数有效性检测 */

if(file==null || !file.isFile()) {

throw new DefaultException("public static void appendFile(File tar) parameters error!");

}

/* 判断文件是否可写 */

if (!file.canWrite()) {

throw new DefaultException(file.toString()
+ " write prohibited! ");

}

/*写文件*/

/*FileWriter fw = new FileWriter(file);

fw.write(js);

fw.close();*/

//根据文件名生成对应格式

if(xml.contains("<pie>")){

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");

out.write(xml);

out.flush();

out.close();

}else{

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"GB2312");

out.write(xml);

out.flush();

out.close();

}

}

else

{

/* 参数有效性检测 */

if(file==null || !file.isFile()) {

throw new DefaultException("public static void appendFile(File tar) parameters error!");

}

/* 判断文件是否可写 */

if (!file.canWrite()) {

throw new DefaultException(file.toString()
+ " write prohibited! ");

}

/*写文件*/

/*FileWriter fw = new FileWriter(file);

fw.write(js);

fw.close();*/

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"GB2312");

out.write(xml);

out.flush();

out.close();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: