我有一个文件夹里面全是音乐文件,我要用XML文件把他们的路径和名字全部整理出来,求一个类!或者EXE程序也可
这种格式的XML就可以不用复杂
<sound1>
   <name>123</name>
   <url>sound/111111.mp3</url>
</sound1>
<sound2></sound2>.....

解决方案 »

  1.   

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;public class Demo {
    //xml保存路径
    private static String path = "E:" + java.io.File.separator + "data.xml"; public static void main(String[] args) {
    //文件所在路径
    Map<Integer, String> map = readfile("D:/QMDownload/SoftMgr", null);
    createXml(map);
    System.out.println("ok");
    } private static void createXml(Map<Integer, String> map) {
    Element root, sound, name, url; root = new Element("music"); for (int i = 0; i < map.size(); i++) {
    sound = new Element("sound");
    sound.setAttribute("id", i + "");

    name = new Element("name");
    url = new Element("url");

    name.setText(i+1+"");
    url.setText(map.get(i));

    sound.addContent(url);
    sound.addContent(name);
    root.addContent(sound);
    }
    Document doc = new Document(root); saveXML(doc); } /**
     * 保存XML
     * 
     * @param doc
     */
    private static void saveXML(Document doc) {
    try {
    XMLOutputter XMLOut = new XMLOutputter();
    Format f = Format.getPrettyFormat();
    f.setEncoding("utf-8");
    XMLOut.setFormat(f);
    XMLOut.output(doc, new FileOutputStream(path));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } /**
     * 
     * 读取某个文件夹下的所有文件夹和文件, 返回所有文件名
     * 
     * @param filepath
     *            String
     * @throws FileNotFoundException
     * @throws IOException
     * @return Map<Integer, String> pathMap
     * 
     */
    public static Map<Integer, String> readfile(String filepath,
    Map<Integer, String> pathMap) {
    try {
    if (pathMap == null) {
    pathMap = new HashMap<Integer, String>();
    } File file = new File(filepath);
    // 文件
    if (!file.isDirectory()) {
    pathMap.put(pathMap.size(), file.getPath()); } else if (file.isDirectory()) { // 如果是目录, 遍历所有子目录取出所有文件名
    String[] filelist = file.list();
    for (int i = 0; i < filelist.length; i++) {
    File readfile = new File(filepath + "/" + filelist[i]);
    // System.out.println("文件名:" + readfile.getName());
    if (!readfile.isDirectory()) {
    pathMap.put(pathMap.size(), readfile.getPath()); } else if (readfile.isDirectory()) { // 子目录的目录
    readfile(filepath + "/" + filelist[i], pathMap);
    }
    }
    }
    } catch (Exception e) {
    }
    return pathMap;
    }
    }
    试一下,看看符合你的要求不