java 生成xml文件
import java.awt.Image;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class createXml
{
public static boolean writeXml(String path,String dir,String wpath,String wname)
{
File[] files = ReaderListFiles(path);
boolean flag = false;
if(files!=null)
{
StringBuffer xml = ReaderFilesContent(files,dir);
if(xml!=null)
flag = writeXmlFile(wname,wpath,xml);
}
return flag;
}
private static File[] ReaderListFiles(String path)
{
File file = new File(path);
if(file.exists()&&file.isDirectory())
return file.listFiles();
else if(file.exists()&&file.isFile())
{
File[] files = new File[1];
files[0] = file;
return files;
}
else
return null;
}
private static StringBuffer ReaderFilesContent(File[] files,String dir)
{
StringBuffer txml = new StringBuffer();
for(int i=0;i<files.length;i++)
{
String filename = files[i].getName();
String temp = filename.substring(filename.lastIndexOf(".")+1);
if(temp!=null&&(temp.equals("gif")||temp.equals("jpg")||temp.equals("bmp")||temp.equals("jpeg")||temp.equals("png")))
txml.append(getImgDesc(filename,temp,dir,files[i]));
}
if(txml.length()>0)
{
StringBuffer xml = new StringBuffer("<?xml version='1.0' encoding='GBK'?>\r\n");
xml.append("<firstnode>\r\n");
xml.append(txml);
xml.append("</firstnode>");
return xml;
}
else
return null;
}
private static StringBuffer getImgDesc(String filename,String type,String dir,File file)
{
Image imgSrc;
StringBuffer xml = new StringBuffer();
try {
imgSrc = ImageIO.read(file);
int width = imgSrc.getWidth(null);
int height = imgSrc.getHeight(null);
xml.append("\t<secondnode id=\"").append(filename).append("\">\r\n");
xml.append("\t\t<name>").append(filename).append("</name>\r\n");
相关文档:
1.基本概念的理解
绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:
C:\xyz\test.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个
URL绝对路径。
相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在
......
1.基本概念的理解
绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:
C:\xyz\test.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个
URL绝对路径。
相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在
......
Properties props=System.getProperties(); //系统属性
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
Syste ......
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadSetting {
......