java jdom 生成xml文件,并备份文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.Date;

/**
 * 
 * 
 * @author Administrator
 * @Date Jul 19, 2008
 * @Time 9:46:11 AM
 * @version 1.0
 */
public class ReadCopyFile {

	public static void list(File path) {
		if (!path.exists()) {
			System.out.println("文件不存在!");
		} else {
			if (path.isFile()) {
				if (path.getName().toLowerCase().endsWith(".pdf")
						|| path.getName().toLowerCase().endsWith(".doc")
						|| path.getName().toLowerCase().endsWith(".chm")
						|| path.getName().toLowerCase().endsWith(".html")
						|| path.getName().toLowerCase().endsWith(".htm")) {// 
					System.out.println(path);
					System.out.println(path.getName());
				}
			} else {
				File[] files = path.listFiles();
				for (int i = 0; i < files.length; i++) {
					list(files[i]);
				}
			}
		}
	}

	/**
	 * 
	 * @param source
	 * @param target
	 */
	public static void copy(File source, File target) {
		File tarpath = new File(target, source.getName());
		if (source.isDirectory()) {
			tarpath.mkdir();
			File[] dir = source.listFiles();
			for (int i = 0; i < dir.length; i++) {
				copy(dir[i], tarpath);
			}
		} else {
			try {
				InputStream is = new FileInputStream(source); 
				OutputStream os = new FileOutputStream(tarpath); 
				byte[] buf = new byte[1024];
				int len = 0;
				while ((len = is.read(buf)) != -1) {
					os.write(buf, 0, len);
				}
				is.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Date myDate = new Date();
		DateFormat df = DateFormat.getDateInstance();
		System.out.println(df.format(myDate));
	}

}

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;

public class OutputXMLMessage {
	public static void outputMessage() {
		Element rootElt = new Element("SyncBindServics");
		rootElt.setAttribute("StreamingNo", "sn0001");
		rootElt.setAttribute("TimeStamp", "ts0001");
		rootElt.setAttribute("BindFlag", "bf0001");
		rootElt.setAttribute("Provider", "pr0001");
		
		Element BindServiceElt = new Element("BindService");
		BindServiceElt.setAttribute("SPID", "sp0001");
		BindServiceElt.setAttribute("CPID", "cp0001");
		BindServiceElt.setAttribute("ServiceID", "sid0001");
		BindServiceElt.setAttribute("ProductID", "pid0001");
		rootElt.addContent(BindServiceElt);
		
		
		Element BindSubContentElt = new Element("BindSubContent");
		BindSubContentElt.setAttribute("SubContentID", "SubContentID0001");
		BindServiceElt.addContent(BindSubContentElt);
		String formationPath = "d:\\upload";
		Document doc = new Document(rootElt);
		try {
			File file = new File(formationPath);
			if(!file.isDirectory()) {
				file.mkdirs();
			}
			XMLOutputter XMLOut = new XMLOutputter(Format
					.getCompactFormat().setEncoding("GBK"));
			FileOutputStream fops = new FileOutputStream(formationPath + "\\aa.xml");
			XMLOut.output(doc, fops);
			fops.close();
			//备份文件
			ReadCopyFile copyFile=new ReadCopyFile();
			File backupFile =new File("e:\\backup");
			//如果备份文件夹不存在则创建
			if(!backupFile.isDirectory()){  
				backupFile.mkdirs();
	        }  
			//取得备份文件
			File fileCopy =new File(formationPath+ "\\aa.xml");
			
			copyFile.copy(fileCopy, backupFile);
			
		} catch (FileNotFoundException e) {
			System.out.println("形成文件失败");
		} catch (IOException e) {
			System.out.println("形成文件失败");
		}

	}
	public static void main(String[] args) {
		outputMessage();
	}
}

 

你可能感兴趣的:(java)