Java生成XML文件-封装类

2021/4/9 12:55:08

本文主要是介绍Java生成XML文件-封装类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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();
}
}
}



这篇关于Java生成XML文件-封装类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程